11

I am getting following output on debugger. I am not sure what syntax is missing.

The SQL code is:

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 boils down to:

12-09 12:34:20.782: E/SQLiteLog(6490): (1) near "Week_Id": syntax error
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
Coder
  • 3,090
  • 8
  • 49
  • 85

2 Answers2

24

Try moving your FOREIGN KEY lists to after your variables are created.

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)); 
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
5

According to the SQLite syntax (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 merges declarations and foreign keys.

Sebastian
  • 5,177
  • 4
  • 30
  • 47
  • 1
    Only issue with this approach is it makes it difficult to migrate these tables to other SQL systems if you end up outgrowing SQLites capabilities. Sticking with the standard `FOREIGN KEY` definitions would be recommended if there are plans to move to another SQL system in the future. – bmcminn Aug 09 '18 at 06:58