AUTOINCREMENT 只允许在 INTEGER PRIMARY KEY 上使用 - android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25562508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY - android
提问by David Lasry
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 :
我正在尝试在我的数据库中创建一个表,其 ID 本身是自动递增的,但是每当我尝试将 AUTOINCREMENT 关键字添加到我的查询时,它都会告诉我:
AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
AUTOINCREMENT 只允许用于 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.
我也尝试编写 AUTO_INCREMENT 但后来出现语法错误。
I found out that this is the source of the problem because whenever I try to remove the AUTOINCREMENT word it works fine.
我发现这是问题的根源,因为每当我尝试删除 AUTOINCREMENT 字时,它都可以正常工作。
So... what do you think is the problem?
所以……你觉得问题出在哪里?
回答by Apoorv
You need to add a space between KEY_ID
AND INTEGER
您需要在KEY_ID
AND之间添加一个空格INTEGER
So change
所以改变
+ KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, "
to
到
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
回答by David Lasry
Create table with integer primary key no need to explicitly mention the AUTOINCREMENT
使用整数主键创建表无需明确提及 AUTOINCREMENT
e.g.
例如
CREATE TABLE t1(
a INTEGER PRIMARY KEY,
C TEXT
);
Insert into t1(C) values("Hello");
回答by Umair
Create Table like this put some space before INTEGER ....
像这样创建表在 INTEGER 之前放一些空间....
"CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT)";