【问题标题】:Adding AND Statement to SQL lite Delete Function - Crashing App向 SQLite 删除函数添加 AND 语句 - 崩溃的应用程序
【发布时间】:2018-05-25 01:46:12
【问题描述】:

出于显而易见的原因,我正在尝试删除名字和姓氏条目中的记录。我的尝试如下(My Delete On Click Listener):

delete.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    if (fname.getText().toString().trim().length() == 0 ||
        lname.getText().toString().trim().length() == 0) {
        showMessage("Error", "Please enter First and Last Name");
        return;
    }

    Cursor c = db.rawQuery("SELECT * FROM customer WHERE fname='" + fname.getText() + "' AND lname='" + lname.getText() + "''", null);
    if (c.moveToFirst()) {
        db.execSQL("DELETE FROM customer WHERE fname='" + fname.getText() + "' AND lname='" + lname.getText() + "''");
        showMessage("Success", "Record Deleted");
    }
    else {
        showMessage("Error", "Invalid First and Last Name");
    }
    clearText();
}
});

我收到的错误位于第 72 行,即光标。

我做错了什么?

【问题讨论】:

    标签: sqlite android-studio


    【解决方案1】:

    您应该认真考虑在此处使用准备好的语句,因为它可以处理正确转义 DELETE 语句中的所有文字的问题。由于我不知道您使用的是哪个框架,因此我不会提供其中一个的代码。要立即解决您的问题,请删除出现在姓氏之后的多余单引号:

    String sql = "SELECT * FROM customer WHERE fname = '" + wfname.getText();
    sql += "' AND lname = '" + lname.getText() + "'";
    Cursor c = db.rawQuery(sql, null);
    

    【讨论】:

    • 啊,你是对的单引号。那个小家伙偷偷溜进去;谢谢你!在我看到你的评论之前,我实际上只是注意到了报价的差异!
    • 我在 Android Studio 中工作,在一个关于 SQLite 的非常基本的课程中,所以不太确定准备好的语句,但如果你想提供代码,我不介意在这种情况下学习一个一个
    • 我不知道你是如何连接到 SQLite 的,所以我不能推荐一个。虽然我做过一些 Android 工作,但我从未在 Android 中使用过 SQLite。
    【解决方案2】:

    关于评论:-

    不太确定准备好的语句,但不介意学习 如果您想为一个提供代码,则在这种情况下大约为一个

    这是对您的代码的直接改编,从使用 rawQueryexecSQL 方法转换为准备语句的 querydelete 便捷方法。

    该代码还修复了一个可能会让您感到困惑甚至是实际问题的问题,因为您需要使用 toString 方法(如您之前所用)来获取 EditTexts 中的数据。

        String whereclause = "fname =? AND lname + =?"; //<<<< Where statement with ?'s for arguments
        String[] whereargs = new String[]{
                fname.getText().toString(), // First arg for first ?
                lname.getText().toString()  // Second arg for second ?
        };
        
        Cursor c = db.query(
                "customer", //<<<< table name
                null, //<<<< equates to * (all columns)
                whereclause, //<<<< the WHERE clause (less WHERE keyword)
                whereargs, //<<<< the argumnets to replace the ?'s (will be escaped/enclosed in quotes for you)
                null,
                null,
                null
        );
    
        //<<<<< REPLACED Cursor c = db.rawQuery("SELECT * FROM customer WHERE fname='" + fname.getText() + "' AND lname='" + lname.getText() + "''", null);
        if (c.moveToFirst()) {
            db.delete(
                    "customer",
                    whereclause,
                    whereargs
            );
            //<<<<< REPLACED db.execSQL("DELETE FROM customer WHERE fname='" + fname.getText() + "' AND lname='" + lname.getText() + "''");
            showMessage("Success", "Record Deleted");
        } else {
            showMessage("Error", "Invalid First and Last Name");
        }
    

    注意事项:-

    • 您应该使用toString 方法来获取EditText 的内容。

    附加

    但是,delete 方法为了方便返回已删除的行数,作为 int,查询是否存在是多余的,所以上面可以简化为:-

        String whereclause = "fname =? AND lname + =?"; //<<<< Where statement with ?'s for arguments
        String[] whereargs = new String[]{
                fname.getText().toString(), // First arg for first ?
                lname.getText().toString()  // Second arg for second ?
        };
        if (db.delete(
                    "customer",
                    whereclause,
                    whereargs) > 0) {
            showMessage("Success", "Record Deleted");
        } else {
            showMessage("Error", "Invalid First and Last Name");
        } 
    

    您不妨考虑查看以下内容以获取更多信息:-

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-17
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2020-07-02
      • 2018-06-04
      • 1970-01-01
      相关资源
      最近更新 更多