代码之家  ›  专栏  ›  技术社区  ›  ttattini

向SQL Lite删除函数添加和语句-崩溃应用程序

  •  0
  • ttattini  · 技术社区  · 7 年前

    出于明显的原因,我正试图删除名字和姓氏条目上的记录。我的尝试如下(我的单击时删除侦听器):

    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行,即光标上。

    我怎么做不正确?

    2 回复  |  直到 7 年前
        1
  •  1
  •   MikeT    7 年前

    关于评论:

    对准备好的陈述不太确定,但不介意学习 如果您想为其中一个提供代码

    下面是对您的代码的直接改编,它从使用 rawQuery execSQL 方法 query delete 准备语句的便利方法。

    代码还修复了一个可能会使您困惑的问题,甚至是您需要使用的实际问题。 toString

        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的内容。

    附加

    不过,为了方便起见 删除 方法返回已删除的行数,作为一个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");
        } 
    

    您可以考虑查看以下内容了解更多信息:

        2
  •  2
  •   Tim Biegeleisen    7 年前

    您应该认真考虑在这里使用一个准备好的语句,因为它将处理正确地转义 DELETE 声明。因为我不知道您使用的是哪种框架,所以我不会给出一种框架的代码。要立即解决您的问题,请删除姓氏后出现的额外单引号:

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