Syntax error with FOREIGN KEY in CREATE TABLE

I get the following output in the debugger. I am not sure which syntax is missing.

SQL code:

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

The error comes down to:

 12-09 12:34:20.782: E/SQLiteLog(6490): (1) near "Week_Id": syntax error 
+6
source share
2 answers

Try moving the FOREIGN KEY lists after creating the variables.

 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)); 
+15
source

According to the syntax of SQLite (http://www.sqlite.org/lang_createtable.html) you can also write something like this:

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

This combines declarations and foreign keys.

+2
source

All Articles