Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
850 views
in Technique[技术] by (71.8m points)

sqlite - AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY - android

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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, "

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...