无法从SQLite数据库中删除行

时间:2018-01-15 07:43:33

标签: java android sql sqlite android-sqlite

我在新的波士顿视频教程之后创建了一个SQLite数据库应用程序。一切都工作正常,除了我不能从数据库或表中删除任何行。当我输入产品的名称并按下删除按钮时,该应用程序应该删除产品,但是当我这样做时,没有任何反应。

这是我的产品类(Products.java)

public class Products {

    private  int _id;
    private String _productname;

    public Products() {
    }

    public Products(String _productname) {
        this._productname = _productname;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public void set_productname(String _productname) {
        this._productname = _productname;
    }

    public int get_id() {
        return _id;
    }

    public String get_productname() {
        return _productname;
    }

处理程序类(MyDBHandler.java)

 public void deleteProduct(String productName){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "=\";" );
    }

    public String databasetoString(){
        String dbString = "";
        SQLiteDatabase db= getWritableDatabase();
        String query = "SELECT * FROM " +  TABLE_PRODUCTS  + " WHERE 1 ";

        Cursor cursor = db.rawQuery(query,null);

        cursor.moveToFirst();

        while(!cursor.isAfterLast()){
            if(cursor.getString(cursor.getColumnIndex("productname"))!=null){
                dbString += cursor.getString(cursor.getColumnIndex("productname"));
                dbString += "\n";
            }
            cursor.moveToNext();
        }
        db.close();
        return dbString;
    }

MainActivity

 public void deleteButtonClicked(View view){
        String inputText= buckysInput.getText().toString();
        dbHandler.deleteProduct(inputText);
        printDatabase();
    }

    public void printDatabase(){
        String dbString = dbHandler.databasetoString();
        buckysText.setText(dbString);
        buckysInput.setText("");
    }

1 个答案:

答案 0 :(得分:0)

您的查询应为。

display:flex