Kala J
Kala J

Reputation: 2070

How can I update the values in my sqlite database?

Here's the sql query I want to run:

 Cursor cursor = sqLiteDatabase.rawQuery("UPDATE collection SET datetime=\""  + (newDate) + "\"" + " WHERE region =" + region, null);

But what am I supposed to do with the cursor to call this query to update my database?

Upvotes: 0

Views: 62

Answers (2)

Blue_Alien
Blue_Alien

Reputation: 2177

for updating database:

SQLiteDatabase sqLiteDatabase= this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("datetime", newDate);

    // updating row
    // if region is string! otherwise convert it into string

    sqLiteDatabase.update(collection, values, "region" + " = ?",
            new String[] { region });

Upvotes: 1

Jibran Khan
Jibran Khan

Reputation: 3256

Use sqLiteDatabase.execSQL() instead. rawQuery is used with SELECT operation

String updateQuery = "UPDATE collection SET datetime= '"  + (newDate) + "' WHERE region = '" + region;
                    sqLiteDatabase.execSQL(updateQuery);

Upvotes: 1

Related Questions