SQLite updates won't save or can't execute
I'm trying to run a SQL update, and it's returning errors. For the life of me I can't figure out what's going wrong. This first attempt errors out with a database can't be opened error, which makes no sense since the second try opens fine:
SQLiteDatabase db = null;
try{
db = SQLiteDatabase.openDatabase(dbnametmp, null, SQLiteDatabase.OPEN_READWRITE);
for (int x = 0; x < ids.length; x++) {
ContentValues values = new ContentValues();
values.put("color", colors[x]);
db.update("ColorTable", values, "id=?", new String[] {Integer.toString(ids[x])});
}
}catch(SQLiteException e){
Log.e("nowsciColor", e.getMessage(), e);
}catch(Exception e){
Log.e("nowsciColor", e.getMessage(), e);
}
if (db != null)
db.close();
The second attempt is below. It does not throw an error, however the actual updates do not occur. If I query the table right after开发者_如何学JAVA (both without and with re-opening the DB), the values are the same as they were prior to the update. I have also tried this without the begin and commits, and as separate sql calls.
SQLiteDatabase db = null;
try{
db = SQLiteDatabase.openDatabase(dbnametmp, null, SQLiteDatabase.OPEN_READWRITE);
String sql = "begin transaction;";
for (int x = 0; x < ids.length; x++) {
sql += "update ColorTable set color=" + Integer.toString(colors[x]) + " where id=" + Integer.toString(ids[x]) + ";";
}
sql += "commit;";
db.execSQL(sql);
}catch(SQLiteException e){
Log.e("nowsciColor", e.getMessage(), e);
}catch(Exception e){
Log.e("nowsciColor", e.getMessage(), e);
}
if (db != null)
db.close();
Running these exact same queries in the DB on a computer works fine. The database is also not in use by any other application. Any help would be appreciated.
@Mimisbrunnr: Permissions are good, in fact the App is running with su.
@st0le: I tried as individual and combined statements with the same results, and exceptions weren't firing on the second method, just the first, which was a could not open db.
@CommonsWare: While adding in begin/set/end code, I realized there was one message not printing in the LogCat with my tag so it was slipping past me:
12-27 14:19:21.043: ERROR/Database(1075): Failure 14 (unable to open database file) on 0x2909b0 when executing 'BEGIN EXCLUSIVE;'
This database file was copied from one that was in use. However, the phone was rebooted and it still has this message. How could the file still be locked? Is there some temporary file somewhere I need to remove or flag I need to clear?
Thanks!
精彩评论