SQLITE INSERT with manual identifier (primary key)

Is it possible to insert into the SQLITE database using the db.insert method with the value of the identifier manual ID [PRIMARY KEY AUTO INCREMENT]? Or should I do this using the rawQuery method using the SQL command?

mylibman ... // Is a custom class containing all values
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(KEY_ID, mylibman.getBookid()); //If I put this line, ID always becomes zero 
values.put(KEY_NAME, mylibman.getBookname());
values.put(KEY_PRINT_NAME, mylibman.getPrintname());
long numrow = db.insert(TABLE_NAME, null, values);
db.close();

Thanks,

+4
source share
1 answer

Yes, you can explicitly define a value for a column INTEGER PRIMARY KEY AUTOINCREMENT. If you try to reinsert the existing key, you will receive the message "PRIMARY KEY must be unique."

Automatic row generation occurs only if the column value is not specified or specified as NULL.

Link: http://www.sqlite.org/autoinc.html

+9
source

All Articles