Android sqlite更新问题

时间:2013-04-26 17:25:34

标签: android sqlite android-sqlite

如果我的问题太简单,请向我道歉并在我的更新声明中指导我的错误。

我有sqlite将我的记录存储在Android应用程序中。我能够成功插入和选择。但是,在更新期间,我收到了一些错误。

代码:

private static final String TABLE_WIFI = "wifi"; // table name

    private static final String KEY_BSSID = "bssid";
    private static final String KEY_NAME = "name";
    private static final String KEY_LAT = "lat";
    private static final String KEY_LNG = "lng";
    private static final String KEY_ACCURACY = "accuracy";

   public void onCreate(SQLiteDatabase db) 
   {
        String CREATE_WIFI_TABLE = "CREATE TABLE " + TABLE_WIFI + "("
            + KEY_BSSID + " TEXT," + KEY_NAME + " TEXT,"
            + KEY_LAT + " TEXT," + KEY_LNG + " TEXT," + KEY_ACCURACY + " TEXT" +")";
        db.execSQL(CREATE_WIFI_TABLE);
    }

    public void updateLocation(Wifi theWifiLocation)
    {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_LAT, theWifiLocation.getLat());
        values.put(KEY_LNG, theWifiLocation.getLng());
        values.put(KEY_ACCURACY, theWifiLocation.getAccuracy());

        db.update(TABLE_WIFI, values, KEY_BSSID + "=" + theWifiLocation.getBssid(), null);
    }

错误日志:

04-26 22:35:42.334: E/AndroidRuntime(30194): FATAL EXCEPTION: Thread-4130
04-26 22:35:42.334: E/AndroidRuntime(30194): android.database.sqlite.SQLiteException: near ":04": syntax error: , while compiling: UPDATE wifi SET lng=?,accuracy=?,lat=? WHERE bssid=54:04:a6:de:98:0c
04-26 22:35:42.334: E/AndroidRuntime(30194):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-26 22:35:42.334: E/AndroidRuntime(30194):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
04-26 22:35:42.334: E/AndroidRuntime(30194):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:150)
04-26 22:35:42.334: E/AndroidRuntime(30194):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:368)
04-26 22:35:42.334: E/AndroidRuntime(30194):    at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:272)
04-26 22:35:42.334: E/AndroidRuntime(30194):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
04-26 22:35:42.334: E/AndroidRuntime(30194):    at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1919)
04-26 22:35:42.334: E/AndroidRuntime(30194):    at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1870)

任何人都可以帮助我

谢谢。

1 个答案:

答案 0 :(得分:5)

您的bsdid是一个字符串,因此您需要查询需要

db.update(TABLE_WIFI, values, KEY_BSSID + "=\"" + theWifiLocation.getBssid() +"\"", null);

甚至更好

db.update(TABLE_WIFI, values, KEY_BSSID + "=?", new String[]{ theWifiLocation.getBssid() });