Reputation: 35
I'm having some trouble getting my autoincrement to work in SQL in my android app.
public static final String TABLE_NAME = "collections";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title";
public static final String CREATE_STATEMENT = "CREATE TABLE " + TABLE_NAME +
"(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
COLUMN_TITLE + " TEXT NOT NULL" + ")";
Are there any major problems with the create statement that could cause this?
Upvotes: 0
Views: 614
Reputation: 4549
There is no need to add AUTOINCREMENT NOT NULL
to the primary key, As primary key(column) is supposed to be auto-increment and it can't be null.
following is an example of a SQL
query which you may edit to create your own table.
public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + "Your_Table_Name" +
" (" + BaseColumns._ID + " INTEGER PRIMARY KEY," +
"first_column_name" + " TEXT" + "," +
"second_column_name" + " TEXT" + ")";
and BaseColumns
is in interface provided by android in package android.provider
which you may use to create a primary column with name "_id"
Upvotes: 1
Reputation: 1489
When you insert a new value do not define id (it will be automatically generated) just use COLUMN_TITLE.
Even in scheme you can skip id definition, because sqlite use RAWID internally and latter aliases your ID to RAWID.
Upvotes: 0