4

我正在开发一个 android 应用程序,在其中我需要根据某个 where 子句更新表中的列。这是下面的代码,

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

上面的代码在 where 子句为 null 的情况下工作正常,但它在 where 子句设置时抛出了一个强制关闭。我的查询错了吗?

4

3 回答 3

14

您需要转义个人资料名称。因此,您可以添加单个 ' 字符:

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

或者,我会遵循的选项:

db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY, values,Profile_Name_for_weekly +" = ?", new String[] {profilename});
于 2013-09-23T12:59:47.373 回答
2

对于了解如何更新数据库行的更一般帮助,这次文档实际上很有帮助:

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

这个页面也很有帮助:Working with SQLite Database (CRUD operations) in Android

就我而言,我做了一个这样的方法:

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;
}
于 2015-01-08T02:06:57.397 回答
0

试试这个:

db.update("yourtable", cvalue, "Profile_Name_for_weekly="+"'"+profilename+"'", null);
于 2014-02-26T06:38:19.223 回答