我正在尝试创建一个表格,但一遍又一遍地收到相同的错误,我一直在谷歌搜索答案,但没有任何成功。
在 onCreate() 中运行的查询是:
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)
)
我得到的错误是:
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)
我怀疑该错误可能与外键或删除级联有关。两个引用列创建为:
CourseID TEXT PRIMARY KEY and TeacherID TEXT PRIMARY KEY
为了确保使用外键,我将 onOpen 方法编写为:
@Override
public void onOpen(SQLiteDatabase db) {
db.execSQL("PRAGMA foreign_keys = ON;");
super.onOpen(db);
}
任何帮助或建议表示赞赏。