Skip to content
Advertisement

SQLite does not update the date value

I am trying to update the date value in the database which is hold as a text, however I stucked here, it takes the user correctly, but does not update the current date;

    public int getID(String userName) {
        String colums[] = {userName};
        SQLiteDatabase db = this.getReadableDatabase();
        String sql = "SELECT * FROM user WHERE userName=?";
        Cursor cursor = db.rawQuery(sql,colums);
        cursor.moveToFirst();
            if(cursor.getCount() > 0){
            int id = cursor.getInt(0);
            db.close();
            cursor.close();
            return id;
        }
        db.close();
        cursor.close();
        return 0;
    }

The part that I try to update the date, the app is closed suddenly;

    public void update(User user, String currentDate){

        SQLiteDatabase db = this.getWritableDatabase();
        int ID =  getID(user.getUserName());
        String strSQL = "UPDATE user SET date = " + currentDate + " WHERE id =?" + ID ;
        db.execSQL(strSQL);
        db.close();
    }

Advertisement

Answer

String strSQL = "UPDATE user SET date = " + currentDate + " WHERE id =?" + ID ;

Seems like SYNTAX (May be Quotes) issue costs you. You can try with

 public void update(User user, String currentDate){

    int user_id =  getID(user.getUserName());
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues data=new ContentValues();
    data.put("date",currentDate);
    db.update("user", data, "id=" + String.valueOf(user_id), null);
    db.close();
 }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement