Question

I encountered some exceptions while writing my DatabaseHandler, I've corrected most of the errors, but this one appeared recently when I though I was just done with debbuging.

This is my SQL statement. All 3 other tables give no error and are created correctly. This is the last create table statement.

private static final String CREATE_COURSE_TABLE = "CREATE TABLE "
        + TABLE_COURSE + " (" + KEY_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_COURSEPROF_ID
        + " INTEGER, " + FOREIGN_PROF + ", " + KEY_COURSEGPA_ID
        + " INTEGER, " + FOREIGN_GPA + ", " + KEY_COURSETERM_ID
        + " INTEGER, " + FOREIGN_TERM + ", " + KEY_NAME + " TEXT, "
        + KEY_COURSE_NUMBER + " TEXT, " + KEY_COURSE_LOCATION + " TEXT";

Also getting this other error before crashing.

Couldn't open AchieveGPA_DB for writing (will try read-only)

Edit:

Thanks for the response, added the missing parentheses. Though the error persists, here is the log:

04-07 17:34:20.213: E/SQLiteOpenHelper(9100): android.database.sqlite.SQLiteException: near "course_gpa_id": syntax error: , while compiling: CREATE TABLE courseTable (_id INTEGER PRIMARY KEY AUTOINCREMENT, course_prof_id INTEGER, FOREIGN KEY(course_prof_id) REFERENCES professorTable(_id), course_gpa_id INTEGER, FOREIGN KEY(course_gpa_id) REFERENCES gpaTable(_id), course_term_id INTEGER, FOREIGN KEY(course_term_id) REFERENCES termTable(_id), name TEXT, number TEXT, location TEXT)

FOREIGN KEY STATEMENTS

// Foreign key statements.
private static final String FOREIGN_PROF = "FOREIGN KEY("+KEY_COURSEPROF_ID+") REFERENCES "+TABLE_PROFESSOR+"("+KEY_ID+")";
private static final String FOREIGN_GPA = "FOREIGN KEY("+KEY_COURSEGPA_ID+") REFERENCES "+TABLE_GPA+"("+KEY_ID+")";
private static final String FOREIGN_TERM = "FOREIGN KEY("+KEY_COURSETERM_ID+") REFERENCES "+TABLE_TERM+"("+KEY_ID+")";
Was it helpful?

Solution

You're mixing column specifications and table constraints (the foreign key declarations). SQL syntax requires that column specifications are first and the table constraints last.

To fix it, move the FOREIGN KEY declarations last after all column specifications.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top