问题
I've got a running database but the primary key won't auto increment. Does anyone know what the problem could be?
The code is as following
// Database name and Version
private static final String DATABASE_NAME = "kmky_database.db";
private static final int DATABASE_VERSION = 1;
// Database Table
private static final String TABLE_NAME = "logs";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_PHONENUMBER = "phonenumber";
private static final String COLUMN_TYPE = "type";
private static final String COLUMN_DATE = "timestamp";
private static final String COLUMN_INCOMING = "incoming";
private static final String COLUMN_OUTGOING = "outgoing";
private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY, " + COLUMN_PHONENUMBER + " TEXT, " + COLUMN_TYPE + " TEXT, " + COLUMN_DATE + " INTEGER, " + COLUMN_INCOMING + " INTEGER, " + COLUMN_OUTGOING + " INTEGER);";
回答1:
You have to create the Primary key as this
...."(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT.....
You forgot the AUTOINCREMENT
key word thats all.
来源:https://stackoverflow.com/questions/19209222/auto-increment-not-working-in-android-sqlite-database