11

我在调试器上得到以下输出。我不确定缺少什么语法。

SQL 代码是:

CREATE TABLE weeks(Week_Id INTEGER PRIMARY KEY, 
  Day TEXT, 
  Start_Time Text, 
  End_Time Text, 
  Break_Time Text );
CREATE TABLE projects(Project_Id INTEGER PRIMARY KEY,
  Name TEXT, 
  Description Text, 
  Client_Name Text, 
  Location Text );  
CREATE TABLE timesheets(Timesheet_Id INTEGER PRIMARY KEY,
  Project_Id  INTEGER,
  FOREIGN KEY (Project_Id) REFERENCES projects (Project_Id),
  Week_Id INTEGER,
  FOREIGN KEY (Week_Id) REFERENCES weeks (Week_Id));  

错误归结为:

12-09 12:34:20.782: E/SQLiteLog(6490): (1) near "Week_Id": syntax error
4

2 回答 2

23

尝试将FOREIGN KEY列表移动到创建变量之后。

CREATE TABLE timesheets(Timesheet_Id INTEGER PRIMARY KEY,
  Project_Id  INTEGER,
  Week_Id INTEGER,
  FOREIGN KEY (Project_Id) REFERENCES projects (Project_Id),
  FOREIGN KEY (Week_Id) REFERENCES weeks (Week_Id)); 
于 2012-12-09T13:03:22.583 回答
5

根据 SQLite 语法 (http://www.sqlite.org/lang_createtable.html) 你也可以这样写:

CREATE TABLE timesheets(Timesheet_Id INTEGER PRIMARY KEY,
Project_Id  INTEGER REFERENCES projects (Project_Id),
Week_Id INTEGER REFERENCES weeks (Week_Id));

这合并了声明和外键。

于 2012-12-09T14:09:21.860 回答