I have a table with a name and a number (debt) what i want to do is when the user inputs a new number to an existing name that number gets added to the initial number, it first checks if the name exists, if its not then it INSERT a new row - this works -
if the name doesn't exists it should UPDATE the existing debt as explained above - and it doesn't work, the update is where i need the help with
right now i'm getting an SQLiteException , from what i understand is that the WHERE in the update isn't good, tryed a couple ways to fix this but none work, still getting exception
this is the relevant code: first the method from the database helper class that the exception points too as the main problem:
public boolean updateExistingFriend(String name, String debt, String date) {
String where = KEY_NAME + "=" + name;
ContentValues newValues = new ContentValues();
newValues.put(KEY_NAME, name);
newValues.put(KEY_DEBT, debt);
newValues.put(KEY_DATE, date);
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
this is the method in the main activity:
private void updateExistingDebt(String name){
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy");
String mydate = df.format(c.getTime());
String whosDebt;
Cursor curs = myDb.getByName(name);
String addedDebt = curs.getString(curs.getColumnIndex("debt"));
if (checkFriendDebt.isChecked()) {
whosDebt = "+" + debtAmount.getText().toString()+ addedDebt +currency.getSelectedItem().toString();
}else{
whosDebt = "-" + debtAmount.getText().toString()+ addedDebt +currency.getSelectedItem().toString();
}
Cursor cursor = myDb.getExistingDebtName(name);
if (cursor.moveToFirst()){
String frName = friendName.getText().toString();
myDb.updateExistingFriend( frName, whosDebt, mydate);
myDb.insertHistory(frName, whosDebt, mydate, debtLabel.getText().toString());
}
cursor.close();
populateListView();
totalDebts();
}
exception in logcat:
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: android.database.sqlite.SQLiteException: no such column: Tal (code 1): , while compiling: UPDATE maindebt SET date=?,name=?,debt=? WHERE name=Tal
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:897)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:508)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1684)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1632)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at com.barados.myfriendsdebts.DBAdapter.updateExistingFriend(DBAdapter.java:203)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at com.barados.myfriendsdebts.MainActivity.updateExistingDebt(MainActivity.java:259)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at com.barados.myfriendsdebts.MainActivity.access$000(MainActivity.java:44)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at com.barados.myfriendsdebts.MainActivity$1$5.onClick(MainActivity.java:196)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.view.View.performClick(View.java:4764)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19844)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5349)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
10-07 05:24:50.890 26063-26063/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
i think the problem is here:
String where = KEY_NAME + "=" + name;
but didn't find a better solution anywhere.
Aucun commentaire:
Enregistrer un commentaire