How to make AUTO_INCREMENT on Android SQLite database?
You don't have to specifically write AUTO_INCREMENT
as in MYSQL.
Just have to define the primary key Field as _id INTEGER PRIMARY KEY
.
It will not work, if have define the Primary key field with INT
when creating the table. It Should be INTEGER
and thats it.
When you don't pass any value for the primary key filed when inserting records, it will automatically increase the value to be a unique value( same way MYSQL
AUTO_INCREMENT
works )
You don't have to parse anything. If the column was created as AUTOINCREMENT
, just pass the other values:
db.execSQL("insert into "
+ TABLE_NAME
+ "(contact_id, contact_name, number_type, contact_number, duration, date, current_time, cont) "
+ "values( "+ cId + ", " + cName + ", " + numType + ", "
+ cNum + ", " + dur + ", " + date + ", " + currTime + ", ? )");
By the way, it's always recommended to insert data using the Android's insert
method:
ContentValues values = new ContentValues();
values.put("contact_id", cId);
values.put("contact_name", cName);
// etc.
db.insert(TABLE_NAME, null, values);
this.db.insert(NULL , 1, contactName, numType, contactNumber,
duration, callDate, currTime);