update sql database with ContentValues and the update-method

后端 未结 4 1962
醉酒成梦
醉酒成梦 2020-11-30 09:55

I would like to update my SQL lite database with the native update-method of the SQLiteDatabase class of android.

ContentValues dataToInsert = new ContentVal         


        
相关标签:
4条回答
  • 2020-11-30 10:17

    Actually, you just need to add apostrophes to your where clause. So it ought to be:

    String where = "id='" + id + "'"
    

    (note: however, this is not best practice, as it theoretically leaves open to injection attacks)

    0 讨论(0)
  • 2020-11-30 10:19

    I have an other approach

        public boolean updateEmployee(TalebeDataUser fav) {
    
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBHelper.COLUMN_ID, fav.getId());
        contentValues.put(DBHelper.COLUM_AD, fav.getAd());
        contentValues.put(DBHelper.COLUMN_NUMARA, fav.getNumara());
        contentValues.put(DBHelper.COLUMN_YURD_ID, fav.getYurtID());
        contentValues.put(DBHelper.COLUMN_EGITIM_ID, fav.getEgitimTur());
        contentValues.put(DBHelper.COLUMN_TEL, fav.getTel());
        contentValues.put(DBHelper.COLUMN_EMAIL, fav.getEmail());
        contentValues.put(DBHelper.COLUMN_ADDRESS, fav.getAdres());
    
        String whereClause = DBHelper.COLUM_AD + " = ? AND " + DBHelper.COLUMN_NUMARA + " = ? ";
        final String whereArgs[] = {fav.getAd(), String.valueOf(fav.getNumara())};// old nameler taranıyor
        int affectedRows = database.update(DBHelper.TABLE_NAME_OGR, contentValues, whereClause, whereArgs);
        return affectedRows > 0;
    }
    
    0 讨论(0)
  • 2020-11-30 10:35

    Actually what exactly you written is correct. The syntax is correct. But you have to check these. String where = "id" + "=" + id; In the above declaration "id" should be type number and id should be int. And if id is a type of TEXT then follow @Adam javin answer.

    0 讨论(0)
  • 2020-11-30 10:42

    You're using the update function wrong. It should be like this:

    String where = "id=?";
    String[] whereArgs = new String[] {String.valueOf(id)};
    
    db.update(DATABASE_TABLE, dataToInsert, where, whereArgs);
    

    The Strings in the whereArgs array gets substituted in for each '?' in the where variable.

    ie. if you had where = "name=? AND type=? then the first '?' would get replaced by whereArgs[0] and the second by whereArgs[1].

    0 讨论(0)
提交回复
热议问题