添加外键时Sqlite查询中的语法错误

时间:2015-07-21 14:29:14

标签: android sqlite

执行查询时,我的sqlite查询出现语法错误

  db.execSQL("CREATE TABLE "+TABLE_ABC+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, EVENT_MATCH_ID INTEGER, FOREIGN KEY (EVENT_MATCH_ID) REFERENCES "+TABLE_DEF+" (MATCH_ID), EVENT_ID TEXT, EVENT_TYPE TEXT, EVENT_MINUTE TEXT," +
                        "EVENT_TEAM TEXT, EVENT_PLAYER TEXT, EVENT_PLAYER_ID TEXT, EVENT_RESULT TEXT)");

错误是" EVENT_ID"附近的语法错误。有人可以帮我跟踪错误吗?

3 个答案:

答案 0 :(得分:2)

将表限制(例如外键定义)放在列规范之后:

db.execSQL("CREATE TABLE "+TABLE_ABC+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, EVENT_MATCH_ID INTEGER, EVENT_ID TEXT, EVENT_TYPE TEXT, EVENT_MINUTE TEXT," +
                    "EVENT_TEAM TEXT, EVENT_PLAYER TEXT, EVENT_PLAYER_ID TEXT, EVENT_RESULT TEXT, " +
                    "FOREIGN KEY (EVENT_MATCH_ID) REFERENCES "+TABLE_DEF+" (MATCH_ID))");

答案 1 :(得分:2)

表约束在列定义之后:https://www.sqlite.org/lang_createtable.html

试试这个:

ease-in

答案 2 :(得分:1)

Plz试试这个:

db.execSQL("CREATE TABLE "+TABLE_ABC+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, EVENT_MATCH_ID INTEGER, EVENT_ID TEXT, EVENT_TYPE TEXT, EVENT_MINUTE TEXT," +
                    "EVENT_TEAM TEXT, EVENT_PLAYER TEXT, EVENT_PLAYER_ID TEXT, EVENT_RESULT TEXT, FOREIGN KEY (EVENT_MATCH_ID) REFERENCES "+TABLE_DEF+" (MATCH_ID))");