【问题标题】:Why Can't I update the values of the sqlite database为什么我不能更新 sqlite 数据库的值
【发布时间】:2015-10-16 17:26:43
【问题描述】:
  public void onClick(View v) {

            if (btn66 == v) {
                ContentValues value = new ContentValues();
                value.put(DBhelper.Amount, txtBudget.getText().toString());
                value.put(DBhelper.Description, txr.getText().toString());

                if(DBhelper.Amount == null)
                {
                    db = helper.getWritableDatabase();
                    db.insert(DBhelper.TABLE2, null, value);
                    db.close();
                    clearfield();
                    Toast.makeText(this, "Budget add Successfully", Toast.LENGTH_LONG).show();
                    fetchData2();
                    Intent i = new Intent(addbudget.this, MainActivity.class);
                    startActivity(i);

                }
                else{
                    db = helper.getWritableDatabase();
                    db.update(DBhelper.TABLE2, value, "_id "+"="+1, null);
                    db.close();

                    fetchData2();
                    Toast.makeText(this, "Update Successfully", Toast.LENGTH_LONG).show();
                    clearfield();
                }
            }
        }

上面是我添加和更新值到sqlite数据库的代码,在我的代码中包含更新方法后,我的添加功能不起作用,我的更新功能也不起作用。我的代码有什么问题吗,但我没有收到任何错误消息。

这是我的数据库表

     static final String C_ID = "_id";
        static final String Name = "name";
        static final String B_ID = "_id";

        public void onCreate(SQLiteDatabase db){

                db.execSQL("CREATE TABLE " + TABLE1+ "(" +C_ID
                        + " INTEGER PRIMARY KEY AUTOINCREMENT," +Name+ " text unique not null)");

                db.execSQL("CREATE TABLE " + TABLE2+ "(" +B_ID
                        + " INTEGER PRIMARY KEY AUTOINCREMENT," +Description+ " text,"
                        +Amount+ " text, FOREIGN KEY ("+Description+") REFERENCES "+TABLE1+"("+Name+"));");




   }

【问题讨论】:

  • “不工作”是对正在发生的事情的糟糕解释。你有例外吗?发生什么了?还包括一个语言标签(java?)。什么是 DBhelper?你在用什么库?
  • 它没有给出任何异常,当我单击按钮添加值时,它说“更新成功”,并且值也没有被添加到数据库中。 DBhelper 是我的数据库类
  • 你为什么要检查DBhelper.Amount == null。这不是静态字符串变量吗?
  • 您也可以发布您的表架构吗?
  • 对不起,其实我不知道这样是否正确,我需要检查我表中的特定列是否为空,如果为空,我想插入新值,否则我想更新以前的值

标签: java sqlite sql-update add


【解决方案1】:

尝试使用类似的方法检查name 中是否存在具有给定name 的行:

public boolean checkIfRowPresent(String name) {
    SQLiteDatabase db = helper.getWritableDatabase();

    Cursor cursor =
            db.query(DBhelper.TABLE1, null, DBHelper.NAME + "='" + name + "'", null, null, null,
                    null, null);
    boolean ret = false;
    if (cursor.getCount() > 0) {
        ret = true; // There is a row present in table1 with the given name
    }
    db.close();

    return ret;

}

你应该这样称呼它:

checkIfRowPresent(txr.getText().toString())

如果有帮助,请告诉我?

【讨论】:

  • 好的,我怎样才能检查该列是否为空?您能简要解释一下吗?PLZ
  • 抱歉回复晚了,非常感谢,您的回答完美。
  • 不错。那么你可以投票并将其标记为正确答案吗?
  • 您也可以使用upvote下方的复选标记将其标记为正确答案。
猜你喜欢
  • 2021-11-09
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
相关资源
最近更新 更多