android.database.sqlite.SQLiteException:near" code":

时间:2015-08-21 21:07:47

标签: android database sqlite

我正在尝试在TABLENAME1密钥中创建复合主要内容,我想在两个不同的表中使用,即TABLENAME5TABLENAME6这可能吗?我觉得这很烦人SQLiteException请帮助我,我对android的东西很新。这是代码..

public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME1 + " (teacher TEXT,course TEXT,att TEXT,PRIMARY KEY(course, att), code TEXT NOT NULL, bunk INTEGER, attend INTEGER, room TEXT)");
        db.execSQL("CREATE TABLE " + TABLE_NAME2 + " (id INTEGER PRIMARY KEY, title TEXT, notes TEXT)");
        db.execSQL("CREATE TABLE " + TABLE_NAME5 + " (course TEXT,bunkdate LONG,FOREIGN KEY(course) REFERENCES " + TABLE_NAME1 + "(course) ON DELETE CASCADE)");
        db.execSQL("CREATE TABLE " + TABLE_NAME6 + " (course TEXT,attdate LONG,FOREIGN KEY(att) REFERENCES " + TABLE_NAME1 + "(att) ON DELETE CASCADE)");
        db.execSQL("CREATE TABLE " + TABLE_NAME3 + " (id INTEGER PRIMARY KEY AUTOINCREMENT,year INTEGER, month INTEGER, day INTEGER, hour INTEGER, minute INTEGER, title TEXT, type TEXT, status INTEGER, snooze INTEGER, shakemode INTEGER, mathsolver INTEGER, sun INTEGER,mon INTEGER,tue INTEGER, wed INTEGER,thu INTEGER, fri INTEGER,sat INTEGER)");
       }

这里是logcat:

08-22 01:51:44.419: E/SQLiteLog(1528): (1) near "code": syntax error
08-22 01:51:44.429: E/AndroidRuntime(1528): FATAL EXCEPTION: main
08-22 01:51:44.429: E/AndroidRuntime(1528): Process: com.smart.scheduler, PID: 1528
08-22 01:51:44.429: E/AndroidRuntime(1528): android.database.sqlite.SQLiteException: near "code": syntax error (code 1): , while compiling: CREATE TABLE campus (teacher TEXT,course TEXT,att TEXT,PRIMARY KEY(course, att), code TEXT NOT NULL, bunk INTEGER, attend INTEGER, room TEXT)

2 个答案:

答案 0 :(得分:3)

根据documentation,表格约束应该在列定义之后,即所有列之后的PRIMARY KEY

答案 1 :(得分:2)

尝试改变

        db.execSQL("CREATE TABLE " + TABLE_NAME1 + " (teacher TEXT,course TEXT,att TEXT,PRIMARY KEY(course, att), code TEXT NOT NULL, bunk INTEGER, attend INTEGER, room TEXT)");

到此:

db.execSQL("CREATE TABLE " + TABLE_NAME1 + " (teacher TEXT,course TEXT,att TEXT, code TEXT NOT NULL, bunk INTEGER, attend INTEGER, room TEXT,PRIMARY KEY(course, att))");

如果不起作用,请告诉我你的logcat。