SQLite.insert return -1 but data inserted into database
I have a method like this:
protected long put(String tableName, ContentValues values) {
SQLiteDatabase db = (!mInTransaction) ? mHelper.getWritableDatabase() : mDb;
long success = db.insert(tableName, null, values);
return success;
}
This method return -1 but data 开发者_JS百科inserted into database. I checked my data, it's ok. Anybody can help me :(
This is my table:
CREATE TABLE choices ( category_no INTEGER NOT NULL, subcategory_no INTEGER NOT NULL, quiz_no INTEGER NOT NULL, choice_no INTEGER NOT NULL, answer TEXT NOT NULL, content_id INTEGER NOT NULL, PRIMARY KEY ( category_no, quiz_no, choice_no ) );
-1
is returned if error occurred. .insert(...)
returns the row id of the new inserted record. Is this important? Do you have _id
field in your db?
I have check your problem but its working fine .
SQLiteDatabase db = openOrCreateDatabase("test.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
String qry = "CREATE TABLE IF NOT EXISTS choices ( category_no INTEGER NOT NULL, subcategory_no INTEGER NOT NULL, quiz_no INTEGER NOT NULL, choice_no INTEGER NOT NULL, answer TEXT NOT NULL, content_id INTEGER NOT NULL, PRIMARY KEY ( category_no, quiz_no, choice_no ) )";
db.execSQL(qry);
ContentValues cv = new ContentValues();
cv.put("category_no",2);
cv.put("subcategory_no",5);
cv.put("quiz_no",2);
cv.put("choice_no",2);
cv.put("answer",3);
cv.put("content_id",4);
long i = db.insert("choices", null, cv);
Log.d("Values of I = ", "******************* " + i + " ***************");
db.close();
May be you have any other problem I had check your issue by entering different different values and it is working fine for me.If you have any doubt then create a new project and try to run this code.
精彩评论