AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY - android

假如想象 提交于 2019-11-27 03:54:44

问题


I'm trying to create a table in my DB with an ID that is autoincrement itself but whenever I try to add the AUTOINCREMENT keyword to my query it tells me that :

AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY

Here is my query:

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_TASKS + " ( "
            + KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_NOTETITLE + " TEXT, " + KEY_NOTECONTENT + " Text, "
            + KEY_STATUS + " INTEGER)";
    db.execSQL(sql);
}

I have also tried to write AUTO_INCREMENT but then I got syntax error.

I found out that this is the source of the problem because whenever I try to remove the AUTOINCREMENT word it works fine.

So... what do you think is the problem?


回答1:


You need to add a space between KEY_ID AND INTEGER

So change

+ KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, "

to

+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "



回答2:


Create table with integer primary key no need to explicitly mention the AUTOINCREMENT

e.g.

CREATE TABLE t1(
  a INTEGER PRIMARY KEY,
  C TEXT
); 

Insert into t1(C) values("Hello");



回答3:


Create Table like this put some space before INTEGER ....

"CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT)";


来源:https://stackoverflow.com/questions/25562508/autoincrement-is-only-allowed-on-an-integer-primary-key-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!