4

I am developing an android app, in which I need update a column in a table based on the a certain where clause.Here is the code below,

public void updatethekeyofweeklycolumn(String profilename, String keystemp) 
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(Profile_keysofweekly, keystemp);

    db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY, values,Profile_Name_for_weekly +" = "+profilename, null);
}

The above code is working fine with where clause as null, but its throwing a force close with the whereclause is set. Is my Query wrong?

3
  • Dude, why don't you accept the answers people give you? It's not like you're supposed to give them money ... Commented Sep 23, 2013 at 13:03
  • 2
    Dude, relax. I just now saw your answer. Thanks it works. Commented Sep 24, 2013 at 4:45
  • 1
    I have not much knowledge about the api's of stack exchange. All I do is ,vote up if the answer is useful, and accept the answers if its right. I cant accept answers which doesnt work and misguide others , even if I am not paying money right? :) .If the answer turns out right, I would surely appretiate the help. Sorry man, no acceptance for a wrong answer from my side. Commented Sep 24, 2013 at 12:19

3 Answers 3

14

You need to escape profilename. So you either add the single ' character:

db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY, values,Profile_Name_for_weekly +" = '"+ profilename + "'", null);

Or, the option I would follow:

db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY, values,Profile_Name_for_weekly +" = ?", new String[] {profilename});
Sign up to request clarification or add additional context in comments.

1 Comment

shouldn't it be db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY, values,Profile_Name_for_weekly +" = ?", new String[] {profilename}) ?
2

For more general help in understanding how to update a database row, the documentation was actually helpful this time:

SQLiteDatabase db = mDbHelper.getReadableDatabase();

// New value for one column
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_TITLE, title);

// Which row to update, based on the ID
String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
String[] selectionArgs = { String.valueOf(rowId) };

int count = db.update(
    FeedReaderDbHelper.FeedEntry.TABLE_NAME,
    values,
    selection,
    selectionArgs);

This page was also helpful: Working with SQLite Database (CRUD operations) in Android

In my case I made a method like this:

public long updateTime(long rowId) {

    // get current Unix epoc time in milliseconds
    long date = System.currentTimeMillis();

    SQLiteDatabase db = helper.getWritableDatabase(); // helper is MyDatabaseHelper, a subclass database control class in which this updateTime method is resides
    ContentValues contentValues = new ContentValues();
    contentValues.put(MyDatabaseHelper.DATE_TIME, date); // (column name, new row value)
    String selection = MyDatabaseHelper.ID + " LIKE ?"; // where ID column = rowId (that is, selectionArgs)
    String[] selectionArgs = { String.valueOf(rowId) };

    long id = db.update(MyDatabaseHelper.FAVORITE_TABLE_NAME, contentValues, selection,
            selectionArgs);
    db.close();
    return id;
}

Comments

0

Try by this one :

db.update("yourtable", cvalue, "Profile_Name_for_weekly="+"'"+profilename+"'", null);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.