So here's something interesting happening with an attempt to update a SQLite row. What I'm doing is quite simple -- updating a single row to represent a "notebook" page for the user in a larger app. I read from the database many times without issue, and I've checked the DB with isReadOnly() and isDbLockedByCurrentThread(). Both those return false.
Here's code to read text from that row, containing the 'notepad':
private String getTextFromDB(){
String query = "SELECT * FROM my_notes where rowId = 0";
String text = "";
Cursor allText = dbAdapter.getData(query);
if (allText.moveToFirst()){
text = allText.getString(allText.getColumnIndex("notepad"));
Log.d(TAG, "Retrieved text from database: "+text);
}
return text;
}
User makes some changes to the file. Here's the code to save it by updating the row -- I tried both using the ContentProvider tools and making raw SQL commands. Both result in the same thing:
public void updateNotepadRow(String note){
Log.d(TAG, "Note contents: "+note);
// New value for one column
ContentValues values = new ContentValues();
values.put(Notepad.COLUMN_NAME_NOTEPAD, note);
// Which row to update, based on the ID
String selection = Notepad.COLUMN_NAME_ROW_ID + " LIKE ?";
String[] selectionArgs = { String.valueOf(0) };
int count = mDb.update(
Notepad.TABLE_NAME,
values,
selection,
selectionArgs);
/*
mDb.execSQL("UPDATE my_notes SET notepad='"+note+"' WHERE rowId = 0");
//check table to see if it was actually updated
String checkQuery = "SELECT * FROM my_notes WHERE rowId = 0";
Cursor checkCur = getData(checkQuery);
if (checkCur.moveToFirst()){
String output = checkCur.getString(checkCur.getColumnIndex("notepad"));
Log.d(TAG, "Output of contents in table my_notes, column notepad: "+output);
}
*/
//check table to see if it was actually updated
String checkQuery = "SELECT * FROM my_notes WHERE rowId=0";
Cursor checkCur = getData(checkQuery);
if (checkCur.moveToFirst()){
String output = checkCur.getString(checkCur.getColumnIndex("notepad"));
Log.d(TAG, "Output of contents in table my_notes, column notepad: "+output);
}
}
Logcat output from this, in which I'm running the update and testing with the indicated query. Looks like everything got saved correctly, right?
D/NotepadActivity:﹕ Notepad text: My field notes: my note!
D/DataAdapter﹕ Note contents: My field notes: my note!
D/DataAdapter﹕ Output of contents in table my_notes, column notepad: My field notes: my note!
Here's the problem, I come back out of that Activity, and when I return to it, here's what I get when I retrieve the contents of the row (BTW: This happens even when I just do an update with no column-value constraints, too):
D/NotepadActivity:﹕ Retrieved text from database: My field notes:
The changes didn't save! Anyone have any idea what's going on?
Aucun commentaire:
Enregistrer un commentaire