Android SQLiteDatabase.update不';不影响行
我正在尝试使用Android SQLiteDatabase.update不';不影响行,android,mysql,sqlite,sqliteopenhelper,Android,Mysql,Sqlite,Sqliteopenhelper,我正在尝试使用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,
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
);
基本上,我想更新所有创建状态的行。
有两个小时没找到解决办法。还有谷歌
有什么想法吗?
谢谢我确认这部分内容:
"%s = %d AND %s = %d"
试试这个:(它们都需要是字符串才能工作)
Log.e(TAG,“success=“+a)
实际上是在您的代码中还是输入错误?这不应该是Log.e(TAG,“success=“+success”)
?是的,这是一个输入错误:)显而易见的答案是没有行与WHERE过滤器匹配。
"%1$s = %2$s AND %3$s = %4$s"