Android SQLite foreign key help needed?
// HANDLER
private static final String SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_CONTENT1 + " text not null);";
private static final String SCRIPT_CREATE_DATABASE2 = "create table " + MYDATABASE_TABLE2 + " (" + KEY_ID2 + " integer foreign key autoincrement, " + KEY_CONTENT2 + " text not null, " + KEY_CONTENT3 + " text not null);";
Any ideas what I might be doing wrong?
// ERROR LOG
> 09-29 11:54:40.052: INFO/Database(484): sqlite returned: error
code =
1, msg = near "foreign": syntax error 09-29 11:54:40.052: ERROR/Database(484): Failure 1 (near "foreign": syntax error) on 0x217ba8 when preparing 'create table MY_TABLE2 (_id2 integer foreign key autoincrement, Content2 text not null, Content3 text not null);'. 09-29 11:54:40.062: DEBUG/AndroidRuntime(484): Shutting down VM 09-29 11:54:40.062: WARN/dalvikvm(484): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): FATAL EXCEPTION: main 09-29 11:54:40.082: ERROR/AndroidRuntime(484): java.lang.RuntimeException: Unable to start activity ComponentInfo{sep.com/sep.com.SepActivity}: android.database.sqlite.SQLiteException: near "foreign": syntax error: create table MY_TABLE2 (_id2 integer foreign key autoincrement, Content2 text not null, Content3 text not null); 09-29 开发者_如何学运维 11:54:40.082: ERROR/AndroidRuntime(484): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at android.os.Handler.dispatchMessage(Handler.java:99) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at android.os.Looper.loop(Looper.java:123) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at android.app.ActivityThread.main(ActivityThread.java:4627) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at java.lang.reflect.Method.invokeNative(Native Method) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at java.lang.reflect.Method.invoke(Method.java:521) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at dalvik.system.NativeStart.main(Native Method) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): Caused by: android.database.sqlite.SQLiteException: near "foreign": syntax error: create table MY_TABLE2 (_id2 integer foreign key autoincrement, Content2 text not null, Content3 text not null); 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at
android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at sep.com.handle$SQLiteHelper.onCreate(handle.java:104) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at
android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at sep.com.handle.openToWrite(handle.java:44) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at sep.com.SepActivity.onCreate(SepActivity.java:38) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 09-29 11:54:40.082: ERROR/AndroidRuntime(484): ... 11 more
// Creating tables
private static final String SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_CONTENT1 + " text not null);";
private static final String SCRIPT_CREATE_DATABASE2 = "create table " + MYDATABASE_TABLE2 + " (" + KEY_ID2 + " integer autoincrement, " + KEY_CONTENT2 + " text not null, " + KEY_CONTENT3 + " text not null, FOREIGN KEY ("+KEY_ID2+") REFERENCES "+PARENT_TABLE+" ("+PARENT_TABLE_PRIMARY+"));";
Foreign key has references
try this ...
String CREATE_MATCH = "CREATE TABLE if not exists "+ TABLE_MATCH
+ "("
+ MatchID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ MatchTeamA + " INTEGER REFERENCES "+TABLE_TEAM+","
+ MatchTeamB + " INTEGER REFERENCES "+TABLE_TEAM+")";
精彩评论