簡體   English   中英

在Android應用程序中使用sqlite更新數據庫

[英]updating database using sqlite in android application

我嘗試使用此功能更新數據庫:

public void onFinish() {
    MyActivity.amountOfVibrations= MyActivity.amountOfVibrations + 1;
    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(500); //0.5 seconds of vibration
    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    db.updateUser( "sampleMail", "132" );
}

updateUser如下:

public void updateUser(String email, String total_usage) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.rawQuery("UPDATE "+ TABLE_LOGIN + " SET "+ KEY_TOTAL_USAGE +" = " + total_usage + " WHERE " + KEY_EMAIL + " = " + email, null);
}

日志給出了這一點:

android.database.sqlite.SQLiteException: no such column: sampleMail (code 1): , while compiling: UPDATE login SET total_usage = 132 WHERE email = sampleMail

我已經通過搜索其他問題來尋找解決方案,但找不到。

我的問題是:為什么沒有這樣的列sampleMail? 我將sampleMail傳遞給updateUser函數。 在數據庫(phpmyadmin)中,sampleMail屬於total_usage列和row email列。

我不明白為什么我會收到此錯誤。 我知道我沒有名為sampleMail的列。 希望此圖像可以澄清它:

圖片顯示列

希望有人可以幫助我。

請嘗試執行此操作,因為查詢正在查找一列而不是您引用的字符串。

public void updateUser(String email, String total_usage) {
    SQLiteDatabase db = this.getWritableDatabase();

    // Create a contentvalues array to store your values that will be updated
    ContentValues values = new ContentValues();
    values.put(KEY_TOTAL_USAGE, total_usage);

    // updating row
    db.update(TABLE_LOGIN, values, KEY_EMAIL + " = '" + email + "'",null);

    db.commit();
    db.close();
}

您可以使用硬編碼查詢嘗試相同的方法嗎? 如果可行,只需將撇號添加到字符串值。

 db.rawQuery("UPDATE login SET total_usage = 132 WHERE email = 'sampleMail';", null);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM