我的表中有一个排序行,以便对数据进行自定义排序。
在我的SQLiteOpenHelper类中,我有一个.update()函数,它调用.update(),在loadCities()中,行被查询,但是我得到了旧的sortId。
下面是我的代码的一部分:
public class TimesHelper extends SQLiteOpenHelper {
private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME
+ " (" + KEY__ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ [...] + KEY_SORTID + " INTEGER);";
private SQLiteDatabase mDB;
private Handler mHandler;
public SQLiteDatabase getDB() {
if (mDB == null) {
mDB = getWritableDatabase();
} else {
mHandler.removeCallbacks(mClose);
}
//useful, but i am not sure wether this is good practise, please comment...
mHandler.postDelayed(mClose, 500);
return mDB;
}
private Runnable mClose = new Runnable() {
@Override
public void run() {
if (mDB != null) {
mDB.close();
mDB = null;
}
}
};
public void drop(int from, int to) {
List<Integer> keys = new ArrayList<Integer>(Times.Cities.keySet());
Integer key = keys.get(from);
keys.remove(key);
keys.add(to, key);
getDB().beginTransaction();
for (Integer i : keys) {
ContentValues val = new ContentValues();
val.put(KEY_SORTID, keys.indexOf(i));
getDB().update(TABLE_NAME, val,
KEY__ID + "=" + i, null);//returning 1
}
getDB().endTransaction();
loadCities();
}
public void loadCities() {
HashMap<Integer, Times> cities = Times.Cities;
cities.clear();
Cursor c = getDB().query(TABLE_NAME, null, null, null, null, null,
KEY_SORTID);
c.moveToFirst();
if (c.isAfterLast()) {
c.close();
return;
}
do {
int s = c.getInt(c.getColumnIndex(KEY_SORTID));
int id = c.getInt(c.getColumnIndex(KEY__ID));
//here i still have the old values...
//do whatever
}
} while (c.moveToNext());
c.close();
}
}我什么都试过了,但没有成功.
梅廷·卡尔
发布于 2014-05-06 18:34:41
你忘了setTransactionSuccessful()。调用endTransaction()而不使用它将回滚事务内所做的任何更改。
处理事务的首选异常安全模式是
beginTransaction();
try {
// db operations...
setTransactionSuccessful(); // didn't throw so far
} finally {
endTransaction(); // rollback or commit
}https://stackoverflow.com/questions/23502167
复制相似问题