I am trying to update the field of an entry in an SQLiteDatabase
using the db.update(...)
method, but it seems the value is not stored. I've tried the convenience db.query(...)
method right after the update method has been executed and found that the entry is still stored as before the update
.
Is there some sort of background work the I must wait for before the query, or where am I going wrong? I am using a singleton extended SQLiteOpenHelper
(dbHelper
) as recommended in SQLite DB accessed from multiple threads and I've even tried getting a new readable instance of the db in a new thread from the helper for the query, as in the code below:
ContentValues deviceListEntry = new ContentValues();
deviceListEntry.put(DeviceListDBEntry.NODE_ID, nodeID);
...
...
String WHERE = DeviceListDBEntry.NODE_ID + " = ?";
final String[] WHERE_ARG = {String.valueOf(nodeID)};
SQLiteDatabase db = dbHelper.getWritableDatabase();
int listings = 0;
try {
//Update the device in the database DeviceList table
listings = db.update(
DeviceListDBEntry.TABLE_NAME,
deviceListEntry,
WHERE,
WHERE_ARG
);
} catch (Exception e) {
throw new ApiHandlerException("db.update(DeviceList, node " + nodeID + ")", e);
}
Log.e("updateDBdevice", " node " + device.getNodeID() + " listening = " + device.isListening());
final String[] TABLE_COLUMNS = {
DeviceListDBEntry.DEVICE_TYPE,
DeviceListDBEntry.INTERVIEWED,
DeviceListDBEntry.DEVICE_JSON
};
final String where = WHERE;
new Thread(new Runnable() {
@Override
public void run() {
SQLiteDatabase db2 = dbHelper.getReadableDatabase();
Cursor deviceEntry = db2.query(
DeviceListDBEntry.TABLE_NAME, //FROM DeviceList Table
TABLE_COLUMNS, //SELECT * columns
where, //WHERE nodeID =
WHERE_ARG, //args nodeID
null,
null,
null
);
if (!deviceEntry.moveToFirst()) throw new ApiHandlerException("DeviceListDB no entry found - WHERE nodeID = " + nodeID);
if (deviceEntry.getCount() > 1) throw new ApiHandlerException("DeviceListDB duplicate entries - WHERE nodeID = " + nodeID);
String deviceJson = deviceEntry.getString(deviceEntry.getColumnIndexOrThrow(DeviceListDBEntry.DEVICE_JSON));
Log.e("updateDBdevice retreive", " node " + nodeID + " JSON : " + deviceJson);
}
}).start();
I am using a Gson object to parse my device
class to a JSON
object which is stored in the DB. I know that this works when using the db.insert(...)
method.
The query here is only there to see if the update was successful, because I found that explicit queries using other delayed threads (synchronised using a object lock and the same SQLiteOpenHelper
) returned values that were not updated.
Is there an obvious thing I am missing or should I consider going to raw SQL commands on the db?
Aucun commentaire:
Enregistrer un commentaire