Android App数据库删除功能不起作用,但添加功能有效

时间:2016-03-25 04:02:57

标签: android

我是Android App的初学者,并从Bucky的教程中学习。对于数据库,我认为我在线教程有相同的代码,但是添加功能工作正常,但删除功能不起作用。

错误:

03-25 03:32:16.401 2212-2212/com.thenewboston.sqlite I/art: Not late-enabling -Xcheck:jni (already on)
03-25 03:32:16.578 2212-2212/com.thenewboston.sqlite W/System: ClassLoader referenced unknown path: /data/app/com.thenewboston.sqlite-1/lib/x86
03-25 03:32:17.216 2212-2238/com.thenewboston.sqlite D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-25 03:32:17.849 2212-2238/com.thenewboston.sqlite I/OpenGLRenderer: Initialized EGL, version 1.4
03-25 03:32:17.997 2212-2238/com.thenewboston.sqlite W/EGL_emulation: eglSurfaceAttrib not implemented
03-25 03:32:17.997 2212-2238/com.thenewboston.sqlite W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabf311e0, error=EGL_SUCCESS
03-25 03:32:18.271 2212-2212/com.thenewboston.sqlite I/Choreographer: Skipped 59 frames!  The application may be doing too much work on its main thread.
03-25 03:32:18.998 2212-2212/com.thenewboston.sqlite I/Choreographer: Skipped 42 frames!  The application may be doing too much work on its main thread.
03-25 03:32:33.633 2212-2212/com.thenewboston.sqlite W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
03-25 03:32:33.648 2212-2212/com.thenewboston.sqlite W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
03-25 03:32:33.648 2212-2212/com.thenewboston.sqlite W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
03-25 03:32:33.649 2212-2212/com.thenewboston.sqlite W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
03-25 03:32:33.650 2212-2212/com.thenewboston.sqlite W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
03-25 03:32:33.651 2212-2212/com.thenewboston.sqlite W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
03-25 03:32:40.513 2212-2223/com.thenewboston.sqlite W/art: Suspending all threads took: 92.247ms

MainActivity:

package com.thenewboston.sqlite;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Handler;
import android.os.Message;

public class MainActivity extends AppCompatActivity {

    EditText willsInput;
    TextView willsText;
    MyDBHandler dbHandler;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            printDatabase();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        willsInput = (EditText) findViewById(R.id.input);
        willsText = (TextView)findViewById(R.id.WillsText);
        dbHandler = new MyDBHandler(this, null, null, 1);
        printDatabase();
    }

    //Add a product to the database
    public void addButtonClicked(View view){
        Runnable r = new Runnable() {
            @Override
            public void run() {
                Products product = new Products(willsInput.getText().toString());
                dbHandler.addProduct(product);
                handler.sendEmptyMessage(0);
            }
        };
        Thread willsThread = new Thread(r);
        willsThread.start();
    }

    //Delete products
    public void deleteButtonClicked(View view){
        Runnable a = new Runnable() {
            @Override
            public void run() {
                String inputText = willsText.getText().toString();
                dbHandler.deleteProduct(inputText);
                handler.sendEmptyMessage(0);
            }
        };
       Thread threada = new Thread(a);
        threada.start();

    }
    public void printDatabase(){
        String dbString = dbHandler.databaseToString();
        willsText.setText(dbString);
        willsInput.setText("");
    }


}

产品类别:

package com.thenewboston.sqlite;

public class Products {

    private int _id;
    private String _productname;

    public Products(){

    }

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

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

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

    public int get_id() {
        return _id;
    }

    public String get_productname() {
        return _productname;
    }
}

MyDBHandler:

package com.thenewboston.sqlite;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;



public class MyDBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String  DATABASE_NAME = "products.db";
    public static final String  TABLE_PRODUCTS = "products";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRODUCTNAME = "productname";

    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_PRODUCTS + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_PRODUCTNAME + " TEXT " +
                ");";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
        onCreate(db);
    }

    //Add a new row to the database
    public void addProduct(Products product){
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCTNAME,product.get_productname());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
    }

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

    //Print out the database as a String
    public String databaseToString(){
        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";

        //Cursor point to a location in your results
        Cursor c = db.rawQuery(query, null);
        //Move to the first row in you results
        c.moveToFirst();

        while(!c.isAfterLast()){
            if(c.getString(c.getColumnIndex("productname")) != null){
                dbString += c.getString(c.getColumnIndex("productname"));
                dbString += "\n";

            }
            c.moveToNext();
        }
        db.close();
        return dbString;
    }
}

有人请帮帮我。我真的不知道bug在哪里。 谢谢! AddButtonClicked完美无缺。唯一的问题是deleteButtonClicked

再次感谢。

1 个答案:

答案 0 :(得分:0)

不要使用" \" for" =",

错误 = db.execSQL(" DELETE FROM" + TABLE_PRODUCTS +" WHERE" + COLUMN_PRODUCTNAME +" = \&#34 ;" + productName +" \&#34 ;;");

CORRECT = db.execSQL(" DELETE FROM" + TABLE_PRODUCTS +" WHERE" + COLUMN_PRODUCTNAME +" =" + productName);

db.delete(" TABLE_PRODUCTS",COLUMN_PRODUCTNAME                 +" =" + productName,null);