0

I am trying to make composite primary in TABLENAME1 key that i want to use in two different tables i.e. TABLENAME5 and TABLENAME6 is this possible? i am getting this annoying SQLiteException please help me with that i am very new to android stuff. Here is the code..

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

and here is 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)
1
  • 1
    you need to define multiple primary keys at the end of the Create table declaration Commented Aug 21, 2015 at 21:15

2 Answers 2

3

According to documentation, table constraints should be after column definitions, i.e. PRIMARY KEY after all columns.

Sign up to request clarification or add additional context in comments.

Comments

2

try to change

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

to this:

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

and let me know your logcat if doesnt work.

6 Comments

i did now it says: android.database.sqlite.SQLiteException: unknown column "att" in foreign key definition (code 1): , while compiling: CREATE TABLE attendance (course TEXT,attdate LONG,FOREIGN KEY(att) REFERENCES campus(att) ON DELETE CASCADE)
att or attdate?, i think you should check the column names
CREATE TABLE attendance (course TEXT,attdate LONG,FOREIGN KEY(attdate) REFERENCES campus(att) ON DELETE CASCADE) i think should be there
"attdate" is a different column in TABLENAME6, and "att" is the one (in TABLENAME1) i am trying to make composite PK along with "course" and use "att" column in TABLENAME6 as foreign key
yes but the TABLENAME6 field is att, and you want to make constraint with TABLENAME1 column attdate, your column names should be as you define in your table. Take a look some samples here alvinalexander.com/android/sqlite-foreign-keys-example
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.