Christian Christian - 1 year ago 69
Android Question

How to skip the primary key when inserting ContentValues in SQLiteDatabase

I have a little Problem with an insert-Statement in my Android-App.

Here is the code:

public void addNote(Note noteItem, int modulNummer){
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_NOTE, noteItem.getNote());
cv.put(COLUMN_NOTEBESCHREIBUNG, noteItem.getBeschreibung());
cv.put(COLUMN_MODULID_FK, modulNummer);
db.insert(NOTETABLE, null, cv);
}


Now my problem. The first column in my table is an auto increment pk. And so i want to skip the first column and i want to begin the insert in the second column. How can i skip this first column?

Update

I've already deleted the .put for the first column. "COLUMN_NOTE" is my second column.
My table-structure looks like this:

id INTEGER AUTO INCREMENT
note double
beschreibung TEXT
modul_id INTEGER


UPDATE 2
I don't know why, but now it works. Thx for your help guys.

Answer Source

If you have a table like the following one:

private final String TAB_GROUP_ADD = "CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, description TEXT NOT NULL);";

And you use the following insert command:

ContentValues values = new ContentValues();
values.put(K_TITLE, title);
values.put(K_DESCRIPTION, description);
db.insert(TAB_GROUP, null, values);

Everything should go fine. The primary key field "id" will no be filled in by Java and the SQLite Database will do it for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download