Java Sqlite数据库更新一行android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18959685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:54:41  来源:igfitidea点击:

Sqlite database updating a row android

javaandroiddatabasesqliteandroid-sqlite

提问by Dave

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,

我正在开发一个 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);
}

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?

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

采纳答案by gunar

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

回答by ritesh4326

Try by this one :

试试这个:

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

回答by Suragch

For more general help in understanding how to update a database row, the documentationwas 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

此页面也很有帮助:在 Android 中使用 SQLite 数据库(CRUD 操作)

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