创建 SQLite 时出现语法错误 Table Android
Syntax error while creating SQLite Table Android
我想对两个表使用外键,现在我的代码如下,但出现语法错误。请解决此错误。提前致谢
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+ DB_TABLE+" ("+
KEY_TBNAME + " TEXT PRIMARY KEY NOT NULL);"
);
db.execSQL("CREATE TABLE " + DB_TABLE_CATE + " (" +
KEY_DETAIL + " TEXT NOT NULL, " +
KEY_AMOUNT + " INTEGER NOT NULL, " +
KEY_RESOURCE + " TEXT NOT NULL, " +
KEY_DATE + " TEXT NOT NULL, "+
KEY_TIME + " TEXT NOT NULL, " +
"FOREIGN KEY ("+KEY_TB_CATE+") REFERENCES "+DB_TABLE+" ("+KEY_TBNAME+");");
}
错误...
java.lang.RuntimeException: Unable to start activity ComponentInfo{app.burhani.quickaccount/app.burhani.quickaccount.HomeActivity}: android.database.sqlite.SQLiteException: near ";": syntax error (code 1): , while compiling: CREATE TABLE tablenamecate (detail TEXT NOT NULL, amount INTEGER NOT NULL, resource TEXT NOT NULL, date TEXT NOT NULL, time TEXT NOT NULL, FOREIGN KEY (tbnameis) REFERENCES tablename (tbname);
这将是真正的查询:
CREATE TABLE DB_TABLE ( KEY_TBNAME TEXT PRIMARY KEY NOT NULL)
CREATE TABLE DB_TABLE_CATE (
KEY_DETAIL TEXT NOT NULL,
KEY_AMOUNT INTEGER NOT NULL,
KEY_RESOURCE TEXT NOT NULL,
KEY_DATE TEXT NOT NULL,
KEY_TIME TEXT NOT NULL,
KEY_TBNAME TEXT NOT NULL,
FOREIGN KEY(KEY_TBNAME) REFERENCES DB_TABLE(KEY_TBNAME));
你必须定义KEY_TBNAME然后设置外键。
我可以在 sqlite 浏览器中 运行 这个查询,它对我有用
你得到 near ";": syntax error
因为你需要两个右括号,一个用于 FK 列名列表,一个用于 table 列列表:
... REFERENCES DB_TABLE(KEY_TBNAME));
您还有另一个错误:FK 中的列 KEY_TB_CATE 必须 也 出现在列列表中。
我想对两个表使用外键,现在我的代码如下,但出现语法错误。请解决此错误。提前致谢
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+ DB_TABLE+" ("+
KEY_TBNAME + " TEXT PRIMARY KEY NOT NULL);"
);
db.execSQL("CREATE TABLE " + DB_TABLE_CATE + " (" +
KEY_DETAIL + " TEXT NOT NULL, " +
KEY_AMOUNT + " INTEGER NOT NULL, " +
KEY_RESOURCE + " TEXT NOT NULL, " +
KEY_DATE + " TEXT NOT NULL, "+
KEY_TIME + " TEXT NOT NULL, " +
"FOREIGN KEY ("+KEY_TB_CATE+") REFERENCES "+DB_TABLE+" ("+KEY_TBNAME+");");
}
错误...
java.lang.RuntimeException: Unable to start activity ComponentInfo{app.burhani.quickaccount/app.burhani.quickaccount.HomeActivity}: android.database.sqlite.SQLiteException: near ";": syntax error (code 1): , while compiling: CREATE TABLE tablenamecate (detail TEXT NOT NULL, amount INTEGER NOT NULL, resource TEXT NOT NULL, date TEXT NOT NULL, time TEXT NOT NULL, FOREIGN KEY (tbnameis) REFERENCES tablename (tbname);
这将是真正的查询:
CREATE TABLE DB_TABLE ( KEY_TBNAME TEXT PRIMARY KEY NOT NULL)
CREATE TABLE DB_TABLE_CATE (
KEY_DETAIL TEXT NOT NULL,
KEY_AMOUNT INTEGER NOT NULL,
KEY_RESOURCE TEXT NOT NULL,
KEY_DATE TEXT NOT NULL,
KEY_TIME TEXT NOT NULL,
KEY_TBNAME TEXT NOT NULL,
FOREIGN KEY(KEY_TBNAME) REFERENCES DB_TABLE(KEY_TBNAME));
你必须定义KEY_TBNAME然后设置外键。 我可以在 sqlite 浏览器中 运行 这个查询,它对我有用
你得到 near ";": syntax error
因为你需要两个右括号,一个用于 FK 列名列表,一个用于 table 列列表:
... REFERENCES DB_TABLE(KEY_TBNAME));
您还有另一个错误:FK 中的列 KEY_TB_CATE 必须 也 出现在列列表中。