Search code examples
androidsqlitesqliteopenhelper

SQLiteException syntax error (code 1):


I am trying to crete a SQLite database in an android application in a SQLiteOpenHelper subclass and I am getting the following error:

.SQLiteException: near "playlist": syntax error (code 1): , while compiling: CREATE playlist(
_id INTEGER PRIMARY KEY,
playlist_name TEXT,
sound_id Integer,
FOREIGN KEY (sound_id) REFERENCES sound(_ID)
);

Here is my relevant code starting with the String constants with the SQL code used to create my databse:

public static final String CREATE_PLAYLIST_TABLE = "CREATE playlist(\n" +
        "   _id INTEGER PRIMARY KEY,\n" +
        "   playlist_name TEXT,\n" +
        "   sound_id Integer,\n" +
        "   FOREIGN KEY (sound_id) REFERENCES sound(_ID)\n" +
        ");";

public static final String CREATE_SOUND_TABLE = "CREATE sound(\n" +
        "   _id INTEGER PRIMARY KEY,\n" +
        "   volume REAL,\n" +
        "   rate REAL,\n" +
        "   FOREIGN KEY (sound_file) REFERENCES sound_file(_ID)\n" +
        ");";

public static final String CREATE_SOUND_FILE_TABLE = "CREATE sound_file(\n" +
        "   _id INTEGER PRIMARY KEY,\n" +
        "   sound_name TEXT,\n" +
        "   is_rate_editing_enabled REAL,\n" +
        "   url TEXT\n" +
        ");";

And here is the onCreate(SQLiteDatabase db) method:

 @Override
public void onCreate(SQLiteDatabase db) {
    Log.d(TAG, "onCreate database called");
    db.execSQL(CREATE_PLAYLIST_TABLE);
    db.execSQL(CREATE_SOUND_FILE_TABLE);
    db.execSQL(CREATE_SOUND_TABLE);
}

Any ideas as far as what is going wrong here?


Solution

  • You need to use the keyword TABLE:

    "CREATE TABLE playlist (" +
        "_id INTEGER PRIMARY KEY, " +
        "playlist_name TEXT, " +
        "sound_id INTEGER, " +
        "FOREIGN KEY (sound_id) REFERENCES sound(_ID))";
    

    Also for the other 2 tables.

    The ; is simply useless.