2

我遇到了这个问题,现在从 3 个小时开始工作,但一切似乎都很好,但事情并没有正常工作,我正在创建表并正确输入数据库表,但插入似乎不起作用并且我收到以下错误:

08-14 15:13:47.590: E/SQLiteDatabase(8606): android.database.sqlite.SQLiteException: near "Code": syntax error (code 1): , while compiling: INSERT INTO login(uid,created_at,Device Code,email,name) VALUES (?,?,?,?,?)
08-14 15:13:47.590: E/SQLiteDatabase(8606):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-14 15:13:47.590: E/SQLiteDatabase(8606):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1012)
08-14 15:13:47.590: E/SQLiteDatabase(8606):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
08-14 15:13:47.590: E/SQLiteDatabase(8606):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-14 15:13:47.590: E/SQLiteDatabase(8606):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-14 15:13:47.590: E/SQLiteDatabase(8606):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-14 15:13:47.590: E/SQLiteDatabase(8606):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
08-14 15:13:47.590: E/SQLiteDatabase(8606):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
08-14 15:13:47.590: E/SQLiteDatabase(8606):     at library.DatabaseHandler.addUser(DatabaseHandler.java:73)
08-14 15:13:47.590: E/SQLiteDatabase(8606):     at com.database_demo.LoginActivity$1.onClick(LoginActivity.java:71)

产生此错误的方法如下:

* Storing user details in database
     * @param string 
     * @param  
     * */
    public void addUser(String name, String email, String uid, String created_at, String DeviceCode) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, name); // Name
        values.put(KEY_EMAIL, email); // Email
        values.put(KEY_UID, uid); // Email
        values.put(KEY_CREATED_AT, created_at); // Created At
        values.put(KEY_Device_Code, DeviceCode);

        // Inserting Row
        db.insert(TABLE_LOGIN, null, values);
        db.close(); // Closing database connection
    }

使用 String 参数 valueres 调用 ABOVE 错误生成方法的条件如下:

if (json.getString(KEY_SUCCESS) != null) {
                            loginErrorMsg.setText("");
                            String res = json.getString(KEY_SUCCESS); 
                            if(Integer.parseInt(res) == 1){
                                // user successfully logged in
                                // Store user details in SQLite Database
                                DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                                JSONObject json_user = json.getJSONObject("user");

                                // Clear all previous data in database
                                userFunction.logoutUser(getApplicationContext());
                                db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT),  json_user.getString(KEY_Device_Code));                     

                                // Launch Dashboard Screen
                                Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

                                // Close all views before launching Dashboard
                                dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(dashboard);

                                // Close Login Screen
                                finish();
                            }else{
                                // Error in login
                                loginErrorMsg.setText("Incorrect username/password");
                            }
                        }

遵循 TARUN 的建议后,我现在收到以下错误:

08-14 16:11:37.925: W/System.err(10277): org.json.JSONException: No value for "Device Code"
08-14 16:11:37.925: W/System.err(10277):    at org.json.JSONObject.get(JSONObject.java:354)
08-14 16:11:37.925: W/System.err(10277):    at org.json.JSONObject.getString(JSONObject.java:510)
08-14 16:11:37.925: W/System.err(10277):    at com.database_demo.LoginActivity$1.onClick(LoginActivity.java:71)
08-14 16:11:37.925: W/System.err(10277):    at android.view.View.performClick(View.java:4223)
08-14 16:11:37.925: W/System.err(10277):    at android.view.View$PerformClick.run(View.java:17275)
08-14 16:11:37.925: W/System.err(10277):    at android.os.Handler.handleCallback(Handler.java:615)
08-14 16:11:37.925: W/System.err(10277):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 16:11:37.925: W/System.err(10277):    at android.os.Looper.loop(Looper.java:137)
08-14 16:11:37.925: W/System.err(10277):    at android.app.ActivityThread.main(ActivityThread.java:4898)
08-14 16:11:37.925: W/System.err(10277):    at java.lang.reflect.Method.invokeNative(Native Method)
08-14 16:11:37.925: W/System.err(10277):    at java.lang.reflect.Method.invoke(Method.java:511)
08-14 16:11:37.925: W/System.err(10277):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-14 16:11:37.925: W/System.err(10277):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-14 16:11:37.925: W/System.err(10277):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

双引号您的KEY_Device_Code列名,因为它包含空格。

Device Code应该"Device Code"

将您的设备代码转义为String KEY_Device_Code= "\"Device Code\"";

于 2013-08-14T10:11:29.420 回答