我正在尝试使用某个表格上的SQLiteDatabase.update
更新多行。
执行查询的代码:
public void onJobStart(int fileType) {
ContentValues cv = new ContentValues();
cv.put(Contract.Entry.UPLOAD_STATUS, STATUS_READY);
String whereClause = String.format(
Locale.US,
"%s = %d AND %s = %d",
Contract.Entry.FILE_TYPE,
fileType,
Contract.Entry.UPLOAD_STATUS,
STATUS_CREATED
);
int success = mDatabase.update(
Contract.Entry.TABLE_NAME,
cv,
whereClause,
null
);
Log.e(TAG, "success=" + success);
}
创建表及其列:
public class DbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "cache.db";
private DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String table = "CREATE TABLE " + Contract.Entry.TABLE_NAME + " ( " +
Contract.Entry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
Contract.Entry.UPLOAD_STATUS + " INTEGER, " +
Contract.Entry.FILE_TYPE + " INTEGER " + ");"
db.execSQL(table);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Contract.Entry.TABLE_NAME);
onCreate(db);
}
}
public final class Contract {
public Contract() {}
public static abstract class Entry implements BaseColumns {
public static final String TABLE_NAME = "files";
public static final String UPLOAD_STATUS = "uploadStatus";
public static final String FILE_TYPE = "fileType";
}
}
此代码不起作用,我在执行查询后不断获取success=0
。这有点奇怪,因为直到昨天,where子句只有一个参数并且它起作用了,即:
String whereClause = String.format(
Locale.US,
"%s = %d",
Contract.Entry.UPLOAD_STATUS,
STATUS_CREATED
);
修改 还试过这个:
String whereClause = Contract.Entry.FILE_TYPE + " = ? AND " + Contract.Entry.UPLOAD_STATUS + " = ?";
String[] whereArgs = new String[] {String.valueOf(fileType), String.valueOf(STATUS_CREATED)};
int success = mDatabase.update(
Contract.Entry.TABLE_NAME,
cv,
whereClause,
whereArgs
);
基本上我想更新其状态创建的所有行。 几个小时没有在SO中找到解决方案。和谷歌。
有什么想法吗? 感谢
答案 0 :(得分:-1)
我在这部分进行了调查:
"%s = %d AND %s = %d"
试试这个:(他们都需要String才能工作)
"%1$s = %2$s AND %3$s = %4$s"
答案 1 :(得分:-1)
一切都看起来井然有序。我会仔细检查条件是否匹配表的至少一行。您还可以尝试使用update
方法以标准方式绑定参数的以下语句:
int success = mDatabase.update(
Contract.Entry.TABLE_NAME,
cv,
Contract.Entry.FILE_TYPE + " = ? AND "
+ Contract.Entry.UPLOAD_STATUS + " = ?",
new String[]{String.valueOf(fileType),String.valueOf(STATUS_CREATED)}
);