SQLiteException:near" user_id":语法错误(代码1)

时间:2018-02-22 14:55:31

标签: android sqlite create-table

创建表时我在user_id附近收到错误但是一切都很好。请帮忙

以下是我的问题:

 String CREATE_USERS_IN_CONVERSATION_TABLE= " CREATE TABLE IF NOT EXISTS "+ UsersInConversationEntry.TABLE_NAME + "( "
            + UsersInConversationEntry.CON_ID + " INTEGER, " + " FOREIGN KEY ("+UsersInConversationEntry.CON_ID+") REFERENCES "+ConversationEntry.TABLE_NAME +"("+ConversationEntry.CON_ID+"), "
            + UsersInConversationEntry.USER_ID + " INTEGER, " + " FOREIGN KEY ("+UsersInConversationEntry.USER_ID+") REFERENCES "+UserEntry.TABLE_NAME +"("+UserEntry.USER_ID+"),"
            + UsersInConversationEntry.HAS_BLOCKED + " TEXT NOT NULL, "
            + UsersInConversationEntry.LAST_UPDATED + " TEXT NOT NULL );";
    db.execSQL(CREATE_USERS_IN_CONVERSATION_TABLE);

我的错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.client.android.voices, PID: 31793
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.client.android.voices/com.client.android.voices.ChatActivity}: android.database.sqlite.SQLiteException: near "user_id": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS users_in_conversation( con_id INTEGER NOT NULL,  FOREIGN KEY (con_id) REFERENCES Conversations(_id), user_id INTEGER,  FOREIGN KEY (user_id) REFERENCES users(_id),has_blocked TEXT NOT NULL, last_updated TEXT NOT NULL );
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                  at android.os.Handler.dispatchMessage(Handler.java:105)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
               Caused by: android.database.sqlite.SQLiteException: near "user_id": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS users_in_conversation( con_id INTEGER NOT NULL,  FOREIGN KEY (con_id) REFERENCES Conversations(_id), user_id INTEGER,  FOREIGN KEY (user_id) REFERENCES users(_id),has_blocked TEXT NOT NULL, last_updated TEXT NOT NULL );
                  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                  at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
                  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
                  at com.client.android.voices.DataBase.VoicesDBHelper.onCreate(VoicesDBHelper.java:39)
                  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:294)
                  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:194)
                  at com.client.android.voices.ChatActivity.onCreate(ChatActivity.java:25)
                  at android.app.Activity.performCreate(Activity.java:6975)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                  at android.os.Handler.dispatchMessage(Handler.java:105) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6541) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

1 个答案:

答案 0 :(得分:0)

con_id INTEGER NOT NULL,FOREIGN KEY(con_id)REFERENCES对话(_id)

那是&#39;,&#39;应该在那里?

修改

以这种方式尝试:

 String CREATE_USERS_IN_CONVERSATION_TABLE= " CREATE TABLE IF NOT EXISTS "+ UsersInConversationEntry.TABLE_NAME + "( "
            + UsersInConversationEntry.CON_ID + " INTEGER, " 
            + UsersInConversationEntry.USER_ID + " INTEGER, " 
            + UsersInConversationEntry.HAS_BLOCKED + " TEXT NOT NULL, "
            + UsersInConversationEntry.LAST_UPDATED + " TEXT NOT NULL,"
+ " FOREIGN KEY ("+UsersInConversationEntry.CON_ID+") REFERENCES "+ConversationEntry.TABLE_NAME +"("+ConversationEntry.CON_ID+"), "
+ " FOREIGN KEY ("+UsersInConversationEntry.USER_ID+") REFERENCES "+UserEntry.TABLE_NAME +"("+UserEntry.USER_ID+")"
 + ");";
    db.execSQL(CREATE_USERS_IN_CONVERSATION_TABLE);