0

I am trying to create a table but keep getting the same error over and over and I have been googling for the answer without any success.

The query that is run in onCreate() is:

CREATE TABLE Posts 
(
    PostID TEXT PRIMARY KEY, 
    CourseID TEXT, 
    FOREIGN KEY (CourseID) REFERENCES Courses (CourseID) ON DELETE CASCADE, 
    PostStartDate INTEGER NOT NULL, 
    PostEndDate INTEGER NOT NULL, 
    TeacherID TEXT, 
    FOREIGN KEY (TeacherID) REFERENCES Teachers (TeacherID) ON DELETE CASCADE, 
    PostDescription TEXT, 
    PostHidden INTEGER DEFAULT(0)
)

The error I get is:

08-04 14:24:24.018: I/com.re.placed.DBHandler(10656): android.database.sqlite.SQLiteException: near "PostStartDate": syntax error (code 1): , while compiling: CREATE TABLE Posts (... (removed the rest of the query)

I suspect that the error might have something to do with either the foreign key or the on delete cascade. The two refenced columns are created as:

CourseID TEXT PRIMARY KEY    and    TeacherID TEXT PRIMARY KEY

And to make sure that foreign keys are used i have written the onOpen method as:

@Override
public void onOpen(SQLiteDatabase db) {
    db.execSQL("PRAGMA foreign_keys = ON;");
    super.onOpen(db);
}

Any help or suggestions are appreciated.

1 Answer 1

2

Try putting the foreign key constraints at the end of the CREATE statement, at least it works in sqlfiddle with sqlite.

CREATE TABLE Posts 
(
    PostID TEXT PRIMARY KEY, 
    CourseID TEXT, 
    PostStartDate INTEGER NOT NULL, 
    PostEndDate INTEGER NOT NULL, 
    TeacherID TEXT, 
    PostDescription TEXT, 
    PostHidden INTEGER DEFAULT(0),
    FOREIGN KEY (TeacherID) REFERENCES Teachers (TeacherID) ON DELETE CASCADE,
    FOREIGN KEY (CourseID) REFERENCES Courses (CourseID) ON DELETE CASCADE
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.