这是我的SQLite方法:
desired_caps = {
'platformName': 'Android',
'deviceName': 'Android Emulator',
'avd': 'emulator',
'autoGrantPermissions': True,
'app': apk_location,
'newCommandTimeout': 36000,
}
这是我的表定义,我已经定义了表列,我想更新fab列: -
public int updateSingleRow(int id, int i){
SQLiteDatabase db = myDBHelper.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("fab", i);
String[] whereArgs = {String.valueOf(id)};
int count = db.update(MyDBHelper.COIN_DETAILS_TABLE ,values,MyDBHelper.UID + "=?",whereArgs);
CoinDetail cd = getDataByID(id);
Log.d("CCP",""+cd.getFab());
return count;
}
答案 0 :(得分:1)
可能是您传递的值(id)作为updateSingleRow
方法的第一个参数,与表中其中一行中的UID列不匹配。
您可以通过暂时修改方法来确定这一点: -
public int updateSingleRow(int id, int i){
SQLiteDatabase db = DatabaseHelper.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("fab", i);
String[] whereArgs = {String.valueOf(id)};
Cursor csr = db.query(MyDBHelper.COIN_DETAILS_TABLE,
null,null,null,null,null,null);
// Possible cause empty table
if (csr.getCount() < 1) {
Log.d("CCP","Table " + MyDBHelper.COIN_DETAILS_TABLE + " is empty.");
return -1;
}
Log.d("CCP","Table " + MyDBHelper.COIN_DETAILS_TABLE +
" contains " + String.valueOf(csr.getCount() + " rows."));
boolean foundflag = false;
StringBuilder idlist = new StringBuilder("Other UID's found :- ");
while(csr.moveToNext()) {
int this_id = csr.getInt(csr.getColumnIndex(MyDBHelper.UID));
if (this_id == id) {
Log.d("CCP"," Matching row found for UID " + String.valueOf(this_id));
foundflag = true;
break;
} else {
idlist = idlist.append(" " + String.valueOf(this_id));
}
}
csr.close();
if (!foundflag) {
Log.d("CCP","Search for UID " + String.valueOf(id) +
" failed. " + idlist);
return -1;
}
int count = db.update(MyDBHelper.COIN_DETAILS_TABLE ,values,MyDBHelper.UID + "=?",whereArgs);
CoinDetail cd = getDataByID(id);
Log.d("CCP",""+cd.getFab());
return count;
}
这将告诉您以下其中一项
如果任何检查表明更新不会完成(空表,没有匹配的UID),它也会返回-1。
简而言之,问题不一定是a或b。如果问题被指示为a或b,那么您需要调查表中为什么没有行(a)或者为什么传递的id与(b)不匹配。
答案 1 :(得分:0)
试试这个
updateContact()
// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_PH_NO, contact.getPhoneNumber());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}