在SQLite中创建表时出错

时间:2015-10-11 06:40:31

标签: android sqlite

我的SQL代码出了什么问题?当我跑步时,应用程序崩溃了。我已经搜索了很长时间但仍然无法弄清楚问题。

     db.execSQL("create table "+TABLE_WORKDETAILS +"(ID INTEGER PRIMARY KEY , Project TEXT, WorkDescription TEXT, Per Text, TimeIn DATETIME,
 TimeOut DATETIME,TotalHours DATETIME, Twf_id INTEGER, FOREIGN KEY(Twf_id) REFERENCES "+TABLE_WORKFORCE+"(ID1),TableInfo_id INTEGER, FOREIGN KEY(TableInfo_id) REFERENCES "+TABLE_INFO+"(ID))");

错误LogCat

 Process: com.example.project.project, PID: 2055
    android.database.sqlite.SQLiteException: near "TableInfo_id": syntax error (code 1): , while compiling: create table WorkDetails(ID INTEGER PRIMARY KEY , Project TEXT, WorkDescription TEXT, Per Text, TimeIn DATETIME, TimeOut DATETIME,TotalHours DATETIME, Twf_id INTEGER, FOREIGN KEY(Twf_id) REFERENCES WorkForce(ID1),TableInfo_id INTEGER, FOREIGN KEY(TableInfo_id) REFERENCES Information(ID))
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)

2 个答案:

答案 0 :(得分:2)

在Sqlite表定义中,所有列定义必须放在键定义之前,而在查询中它们是混杂在一起的。试试这个:

CREATE table WorkDetails(ID INTEGER PRIMARY KEY , 
  Project TEXT, WorkDescription TEXT, Per Text,

  TimeIn DATETIME, TimeOut DATETIME,TotalHours DATETIME,TableInfo_id INTEGER, 
  Twf_id INTEGER, FOREIGN KEY(Twf_id) REFERENCES WorkForce(ID1),
  FOREIGN KEY(TableInfo_id) REFERENCES Information(ID))   

答案 1 :(得分:1)

SQLite website中有一个很酷的图表,可以直观地说明创建表语句必须如何。

enter image description here

因此,正如您所看到的,table-constraints必须在column-def之后。因此,您现在可以将所有外键和主键定义放在语句的底部。