Trying to simple update a row in an SQLiteDatabase Database. I can insert records fine with:
myDB.execSQL("INSERT INTO " + table + " (Date, Urine, Def, Flat, Snez, Mus)" + " VALUES ('" + dat + "', " + uri + ", " + def + ", " + fl + ", " + sn + ", " + mu + ");");
But when I try to update a row with:
myDB.execSQL("UPDATE " + TableMain + " SET " + field+ "=" + number + "WHERE Date = "+ DateMaker(today)+";");
I get a "Database Not Open" error in LogCat. So I try to open the database with:
SQLiteDatabase db=myDB.getWritableDatabase();
But I’m told by eclipse that getWritableDatabase() is undefined, so I tried:
myDB.openDatabase("myDB", null, MODE_PRIVATE);
But then I’m greeted with the error "Unable to open Database File". So I changed the path to data/data/com.BodyTracker/database so..:
myDB.openDatabase("data/data/com.BodyTracker/database", null, MODE_PRIVATE);
And I once again get unable to open database error. I don’t know why it can’t open it, it can write to and read from the database, it just won’t update. This is all happening in my main activity, which extends TabActivity, not SQLiteDatabase. I’m not using DatabaseHelper, I’m new to this and it’s too complicated for my simple needs (reading in and out 6 values, and updating them).
Where I create the database:
myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
/* Create a Table in the Database. */
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ TableMain
+ " (Date VARCHAR, Urine INT(3), Def INT(3), Flat INT(3), Snez INT(3), Mus INT(3));");
My Update Code:
public int UpdateDB(String field, int number)
{
//SQLiteDatabase db=myDB.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("Urine", number);
cv.put("Def", number);
cv.put("Flat", number);
cv.put("Snez", number);
cv.put("Mus", number);
cv.put("Date", DateMaker(today));
myDB.openDatabase("data/data/com.BodyTracker/database", null, MODE_PRIVATE);
// myDB.rawQuery("UPDATE " + TableMain + " SET " + field+ "=" + number + "WHERE Date =? ", new String[]{DateMaker(today)});
// myDB.execSQL("UPDATE " + TableMain + " SET " + field+ "=" + number + "WHERE Date = "+ DateMaker(today)+";");
return myDB.update(TableMain, cv, "Date=?", new String[]{DateMaker(today)});
}
Please ask for other info, this is my first question so not 100% what exactly to give.
Try update command