Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SQLite使用where子句更新项目_Android_Database_Sqlite_Filtering - Fatal编程技术网

Android SQLite使用where子句更新项目

Android SQLite使用where子句更新项目,android,database,sqlite,filtering,Android,Database,Sqlite,Filtering,我需要一些关于如何更新SQLite数据库中的数据的建议。现在我是这样做的: public void insertadoMedidasAdoptarCorrectamente(String codMedAdC, String codigoServicio) { AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this); SQLiteDatabase sqliteDatabase =

我需要一些关于如何更新SQLite数据库中的数据的建议。现在我是这样做的:

public void insertadoMedidasAdoptarCorrectamente(String codMedAdC,
        String codigoServicio) {
    AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this);
    SQLiteDatabase sqliteDatabase = androidOpenDbHelper
            .getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(AndroidOpenDbHelper.insertadoCorrectamente, 1);

    sqliteDatabase.update("MedidasAdoptarSeleccionadas", contentValues,
            "codigomedida = '" + codMedAdC + "' AND codigoservicio = '"
                    + codigoServicio + "'", null);

    sqliteDatabase.close();
}
我听说这不是更新数据的最佳方式。我认为有时它会出错。由于该代码:

sqliteDatabase.update("MedidasAdoptarSeleccionadas", contentValues,
            "codigomedida = '" + codMedAdC + "' AND codigoservicio = '"
                    + codigoServicio + "'", null);

    sqliteDatabase.close();
您可以观察到,我通过两个字段筛选项目。我已经看到,这可以用where子句参数来完成

所以,我的问题是,过滤将要更新的数据的最佳方法是什么

谢谢。

您不应该执行
“codigomedida=”“+codMedAdC+”
,因为如果
codMedAdC
中存在
,则该操作将中断

但是像这样做

String[] whereArgs = { codMedAdC, codigoServicio };
sqliteDatabase.update("MedidasAdoptarSeleccionadas", contentValues,
            "codigomedida = ? AND codigoservicio = ?", whereArgs);
很好

如果定期更新,您可以考虑保持数据库的打开状态。创建新的
AndroidPendbHelper
并每次打开数据库都需要一些时间