尝试在SQLite数据库中添加现有值

时间:2015-10-07 02:41:12

标签: android sqlite

我有一张名字和号码的表(债务) 我想要做的是当用户向现有名称输入一个新号码时,该号码被添加到初始号码, 它首先检查名称是否存在,如果不存在则INSERT新行 - 这是有效的 -
如果名称不存在,它应该UPDATE如上所述的现有债务 - 并且它不起作用,更新是我需要帮助的地方

现在我得到一个SQLiteException,据我所知,更新中的WHERE不好,尝试了几种方法来解决这个问题但没有工作,仍然有异常

这是相关代码: 首先来自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)

我认为问题在于:

  

字符串where = KEY_NAME +“=”+ name;

但在任何地方都找不到更好的解决方案。

1 个答案:

答案 0 :(得分:0)

你认为问题存在是非常正确的。在产生错误的特定实例中,您显然正在处理名为tal的人。 但是

字符串where = KEY_NAME +“=”+ name;

制作以下内容

name = Tal

告诉sqlite比较列名,列Tal显然不存在。您可以使用引号

来附加名称变量

字符串where = KEY_NAME +“='”+ name +“'”;

但这绝对不是正确的答案。正确的答案是修改查询的这一部分,使其看起来像其余部分。

字符串where = KEY_NAME +“=?”;

即使用预准备语句并绑定参数,因为您显然正在为其他字段执行此操作。