3

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 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 ) );

asedra_le
  • 3,079
  • 8
  • 38
  • 56

2 Answers2

8

-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?

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • 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 ) ); – asedra_le Jul 28 '11 at 02:43
  • `CREATE TABLE choices ( _id INTEGER AUTOINCREMENT, 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, _id ) );` If this does not return the something > `-1` then try removing other primary keys besides `_id`. if still returning `-1`, I dont know what it could be. If you dont use the value returned by `put(..)` elsewhere, there is no need to care about that, as long as it inserts data into your db. – Nikola Despotoski Jul 28 '11 at 02:47
  • Thanks for your help Nikala Despotoski. I fixed my problem :) – asedra_le Jul 28 '11 at 04:09
  • @NikolaDespotoski I have a table which don't have `_id` and also there is no `PRIMARY KEY`. then what **row ID** should `insert` return? Thanks – Muhammad Babar Nov 20 '14 at 07:49
0

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.

Dharmendra
  • 33,296
  • 22
  • 86
  • 129