Reputation: 2037
This is my SimpleDBAdapter Class
that encapsulates all the complexities of accessing the database and the inner class SimpleDBHelper
takes care of CRUD operations,now the Problem that I am facing here is that when I deploy the code, the tables were getting created, but I am unable to insert the values in to the table, the id
is returning -1
that depicts that there is an error,while inserting the values.
id = db.insert(TABLE_SIMPLETABLE_CLIENT1,null,contentvalues);
SimpleDBAdapter.java
import android.content.ContentValues; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.view.ViewGroup.MarginLayoutParams; public class SimpleDBAdapter { // Define all the constants that are to used in the Program private static final String DATABASE_NAME = "SimpleDB.db"; private static final String TABLE_SIMPLETABLE_CLIENT1 = "SimpleTable1"; private static final int DATABASE_VERSION = 1; private static final String KEY_EMPLOYEE_ID = "Employee_id"; private static final String KEY_EMPLOYEE_NAME = "Name"; private static final String KEY_EMPLOYEE_DESIGNATION = "Designation"; private static final String KEY_EMPLOYEE_MARITUALSTATUS = "Maritual Status"; private static final String KEY_EMPLOYEE_DOJ = "Date of Joining"; private static final String TAG = "SimpleDBAdapter"; String CREATE_TABLE_SIMPLETABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_SIMPLETABLE_CLIENT1 + " (Employee_ID INTEGER PRIMARY KEY AUTOINCREMENT, " + " Employee_Name VARCHAR(20)," + " JoinedDateTime DATETIME," + " Designation VARCHAR(20)," + " Maritual_Status BOOLEAN DEFAULT 'FALSE');"; private final Context ctx; private SQLiteDatabase db; private SimpleDBHelper simpledbhelper; public SimpleDBAdapter(Context context) { Log.i(TAG,"SimpleDBAdapter Constructor"); ctx = context; simpledbhelper = new SimpleDBHelper(ctx); } class SimpleDBHelper extends SQLiteOpenHelper { private static final String TAG = "SimpleDBHelper"; public SimpleDBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); Log.i(TAG,"Calling.. super(context, DATABASE_NAME, null, DATABASE_VERSION)"); } @Override public void onCreate(SQLiteDatabase db) { Log.i(TAG,"Creating Table named CREATE_TABLE_SIMPLETABLE"); db.execSQL(CREATE_TABLE_SIMPLETABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) { Log.w(TAG, "Upgarding from" + oldversion + "to" + newversion + "could remove all the old data"); db.execSQL("DROP TABLE IF EXISTS TABLE_SIMPLETABLE_CLIENT1"); onCreate(db); } } //Opens the DB public void open() throws SQLException { Log.i(TAG,"Open() Called..."); db = simpledbhelper.getWritableDatabase(); simpledbhelper.onCreate(db); } //closes the DB public void close() { Log.i(TAG,"Close() Called..."); simpledbhelper.close(); } //inserting the values in to Table public long insertEntry(String Name,String Designation,Boolean MaritualStatus,String date){ Log.i(TAG,"Insert Entry ()"); long id = 0; try{ ContentValues contentvalues = new ContentValues(); contentvalues.put(KEY_EMPLOYEE_NAME, Name); contentvalues.put(KEY_EMPLOYEE_DOJ,date); contentvalues.put(KEY_EMPLOYEE_DESIGNATION, Designation); contentvalues.put(KEY_EMPLOYEE_MARITUALSTATUS, MaritualStatus); id = db.insert(TABLE_SIMPLETABLE_CLIENT1,null,contentvalues); } catch(Exception err){ Log.i(TAG,"Error Encountered..."); Log.e(TAG,String.valueOf(err).toString()); } return id; } }
In the
MainClass
file (DBHandler.java), i am creating instance of the code and calling the insert method of this class as follows,DBHandler.java
import android.app.Activity; import android.os.Bundle; import android.util.Log; public class SimpleDBApplication extends Activity { private static final String TAG = "SimpleDBApplication"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SimpleDBAdapter DBAdapter = new SimpleDBAdapter(this); Log.i(TAG,"Called SimpleDBAdapter"); //call to open the DB DBAdapter.open(); long id; // add an entry id = DBAdapter.insertEntry("Vinayaka","CEO",false,"17-7-2010"); Log.w(TAG,"Tuple Inserted"); Log.w(TAG," The Value of ID :" +String.valueOf(id).toString()); //call to close the DB DBAdapter.close(); } }
Thanks in Advance...
Upvotes: 0
Views: 2745
Reputation: 16663
As I can see so far, your ContentValues keys are not the same as the column names, which should be as I recall.
So your constants would have to become;
private static final String KEY_EMPLOYEE_ID = "Employee_ID";
private static final String KEY_EMPLOYEE_NAME = "Employee_Name";
private static final String KEY_EMPLOYEE_DESIGNATION = "Designation";
private static final String KEY_EMPLOYEE_MARITUALSTATUS = "Maritual_Status";
private static final String KEY_EMPLOYEE_DOJ = "JoinedDateTime";
if that doesn't solve your problem, try to post the stacktrace, so that we have more details to help you.
Upvotes: 1
Reputation: 1075
You can get the specific reason if instead of insert()
you use insertOrThrow()
and catch the exception, then the error message should help you figure out the reason quickly.
Upvotes: 2