SQLite异常:Android中没有这样的列

时间:2015-02-19 22:01:57

标签: java android sqlite android-activity android-sqlite

我写了一个应用程序来更新SQlite Db中的行,下面是数据处理程序中更新的代码:

public long updatedata(String ID, String NAME,String NUMBER)
    {
        ContentValues content = new ContentValues();
        content.put(name, NAME);
        content.put(number,NUMBER);
        return db.update(table_name, content, "id "+"="+ID, null);

    }

现在当我需要更新我在代码下面使用的行时:

Datahandler data =new Datahandler();
data.open();
long updateresult = data.updatedata(oldID, cloudnames.get(pos), cloudnumbers.get(pos));
data.close();

还假设上面提供的值存在且有效,而且我确信我的数据库中存在id = jo_2,问题是当我运行代码时,异常如下所示:

02-19 23:59:34.200: E/SQLiteLog(18485): (1) no such column: jo_2
02-19 23:59:34.205: W/dalvikvm(18485): threadid=11: thread exiting with uncaught exception (group=0x41b30c08)
02-19 23:59:34.210: E/AndroidRuntime(18485): FATAL EXCEPTION: IntentService[test-service]
02-19 23:59:34.210: E/AndroidRuntime(18485): Process: com.kaizendroid.bc, PID: 18485
02-19 23:59:34.210: E/AndroidRuntime(18485): android.database.sqlite.SQLiteException: no such column: jo_2 (code 1): , while compiling: UPDATE info SET name=?,number=? WHERE id =jo_2
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1706)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1654)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at com.kaizendroid.bc.DataHandler.updatedata(DataHandler.java:122)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at com.kaizendroid.bc.MyTestService.getbcfromcloud(MyTestService.java:169)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at com.kaizendroid.bc.MyTestService.get_local_cloud_compare(MyTestService.java:70)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at com.kaizendroid.bc.MyTestService.onHandleIntent(MyTestService.java:49)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.os.Looper.loop(Looper.java:146)
02-19 23:59:34.210: E/AndroidRuntime(18485):    at android.os.HandlerThread.run(HandlerThread.java:61)

3 个答案:

答案 0 :(得分:8)

该错误并未表示&#34; jo_2&#34;的值表中缺少,但缺少名为jo_2。听起来你某处有一些格式错误的SQL。

如果这是您的实际SQL(来自错误):

UPDATE info SET name=?,number=? WHERE id =jo_2

然后你需要jo_2部分的引号:

UPDATE info SET name=?,number=? WHERE id = 'jo_2'

如果没有它们,SQLite会将列id jo_2进行比较,而 return db.update(table_name, content, "id = '" + ID + "'", null); // add quotes here ^ and ^ 不存在。

要解决此问题,您可以在代码中更改此行:

{{1}}

编辑Falmarri's answer显示了一种更安全的方法来修复代码以避免错误。

答案 1 :(得分:1)

您可以自己打开SQL注入。使用内置的where子句参数插入。

public int update (String table, ContentValues values, String whereClause, String[] whereArgs)

所以你想要

return db.update(table_name, content, "id = ?", new String[] {"jo_2"});

答案 2 :(得分:0)

我遇到了这个问题,你可以做到这一点

 String originalYearString = uri.getLastPathSegment();
            where= TableName.COL_YEAR_NAME+" = "+"'"+originalYearString+"'";//placing values of the column to be updated in '' is important or else it detects it as a single table column 
            if(!TextUtils.isEmpty(selection)){
                where+=" AND "+selection;
            }
            rowUpdated=database.update(TABLE_NAME,values,where,selectionArgs);
            break;