2012-12-09 157 views
6

我正在下面的調試器輸出。我不確定缺少什麼語法。與外鍵的語法錯誤CREATE TABLE

的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 
+0

有人嗎?請幫助。謝謝 – Coder

+0

您的FOREIGN KEY聲明可能有可能在變量聲明之後。值得一試...... – PearsonArtPhoto

+0

請您詳細說明一下。我沒有明白你的意思。 – Coder

回答

15

請嘗試移動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)); 
+0

謝謝你的工作。這有什麼特別的原因? – Coder

+0

我不是專家,但我猜想FOREIGN KEYs應該是CREATE TABLE字符串中處理的最後一件東西。很高興它的工作:-) – PearsonArtPhoto

+0

謝謝..我也爲我工作 – Pranita

2

據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)); 

這將合併聲明和外鍵。