我有一個名稱和一個數字(債務)表 我想要做的是當用戶輸入一個新的數字到一個現有的名字,數字被添加到初始號,如果 它首先檢查名稱存在,如果它不那麼它INSERT
一個新行 - 這工作 -
如果名稱不存在,它應該UPDATE
現有債務如上解釋 - 它不工作,更新是我需要的幫助試圖添加到SQLite數據庫中的現有值
現在我得到一個SQLiteException
,從我的理解是WHERE
在更新不好,嘗試了幾種方法來解決這個BU牛逼沒有工作,仍然得到例外
這是相關代碼: 首先從DatabaseHelper
類中的方法是異常點也作爲主要的問題:
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;
}
這是在主要活動的方法:
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();
}
例外的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)
我認爲問題在這裏:
String where = KEY_NAME +「=」+ name;
但是在任何地方都找不到更好的解決方案。
很好的解釋!兩種方式擺脫了異常,但只有** String where = KEY_NAME +「='」+ name +「'」; **實際更新了行,「=?」似乎沒有做任何事情,不確定每種方法究竟意味着什麼,但我會環顧四周,無論如何謝謝你:) –
希望這個鏈接將解釋有關參數綁定https://simonwdixon.wordpress.com/2011/08/ 16/android-sqllite-tip-2-use-compiled-sql-statements /(使用?)通常不使用它在android上不是一個大問題,但對於使用PHP等的web應用程序來說,使用參數綁定總是一個好主意。 – e4c5