简体   繁体   中英

updating database using sqlite in android application

I try to update my database by using this function:

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" );
}

The updateUser is as follows:

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);
}

The log gives this:

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

I already searched for a solution by searching other questions, but I could not find it.

My question is: Why it says no such column sampleMail? I pass sampleMail to the updateUser function. In the database(phpmyadmin) sampleMail belongs to the column total_usage and row email .

I dont understand why I get this error. I understand that I have not a column named sampleMail. Hopefully this image will clarify it:

图片显示列

Hopefully someone can help me.

Try this as the query is looking for a column and not your quoted string.

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();
}

Can you try the same with hard coded query. If that works, just add apostrophes to the string values.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM