SQLiteException语法错误(代码1):

时间:2014-12-21 18:36:42

标签: android sqlite sqliteopenhelper

我正在尝试在SQLiteOpenHelper子类中的android应用程序中创建一个SQLite数据库,我收到以下错误:

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

以下是我的相关代码,以String常量开头,其中包含用于创建数据库的SQL代码:

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

这是onCreate(SQLiteDatabase db)方法:

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

关于这里出了什么问题的任何想法?

1 个答案:

答案 0 :(得分:1)

您需要使用关键字TABLE

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

另外2张表。

; 根本没用。