1

I've been grinding my teeth on the following bug:

I am defining a few tables, two of which must share a value "task_id". I am using the following create statement(s) in my code:

// SQL Statement to create new Tasks table
private static final String TASKS_TABLE_CREATE = "create table " + TASKS_TABLE
    + (" (" + KEY_TASKS_TASKID + " integer primary key autoincrement, ")
    + (KEY_TASKS_TASK + " TEXT, ")
    + (KEY_TASKS_SESSION + " TEXT, ")
    + (KEY_TASKS_CONDITION + " TEXT);");
// SQL Statement to create new RAT table
private static final String RAT_TABLE_CREATE = "create table " + RAT_TABLE
    + (" (" + KEY_ID + " integer primary key autoincrement, ")
    + (KEY_RAT_TIMESTAMP + " DATETIME DEFAULT null, ")
    + (KEY_RAT_TASKID + " INTEGER NOT NULL, ")
    + ("FOREIGN KEY(" + KEY_RAT_TASKID + ") REFERENCES ")
    + (TASKS_TABLE + "(" + KEY_TASKS_TASKID + "), ")
    + (KEY_RAT_SESSION + " TEXT DEFAULT null, ")
    + (KEY_RAT_RAT_A + " TEXT DEFAULT null, ")
    + (KEY_RAT_RAT_B + " TEXT DEFAULT null, ")
    + (KEY_RAT_RAT_C + " TEXT DEFAULT null, ")
    + (KEY_RAT_ANSWER + " TEXT DEFAULT null, ")
    + (KEY_RAT_CONDITION + " TEXT DEFAULT null);");

I am getting an SQL exception which says:

06-26 20:24:49.759: E/Database(17404): Failure 1 (near "session": syntax error) on 0x25afb0 when preparing 'create table rat (_id integer primary key autoincrement, timeStamp DATETIME DEFAULT null, taskId INTEGER NOT NULL, FOREIGN KEY (taskId) REFERENCES tasks (taskId), session TEXT DEFAULT null, ratA TEXT DEFAULT null, ratB TEXT DEFAULT null, ratC TEXT DEFAULT null, answer TEXT DEFAULT null, condition TEXT DEFAULT null);'.

I've looked through and can't find anything that addresses my specific problem. Is there a constraint on where in the 'create' statement one can make reference to a foreign key? Thanks for any help given on this issue.

1 Answer 1

1

I believe the statement should be formatted as follows:

col_name1 TEXT REFERENCES table_name ( col_name2 )

So you could try:

KEY_RAT_TASKID + " REFERENCES " + TASKS_TABLE + "(" + KEY_TASKS_TASKID + ")"
1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.