2016-03-25 6 views
1

私はAndroid Appの初心者で、Buckyのチュートリアルから学びます。データベースの場合、私はチュートリアルのオンラインで同じコードを持っていると思うが、Add関数は完全に機能しているが、Delete関数は機能していない。Android Appデータベースの削除機能は動作しませんが、機能の追加は

エラー:

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; 
    } 
} 

誰か助けてください。私は本当にバグがどこにあるのか分からない。 ありがとう! AddButtonClickedは完全に機能します。唯一の問題はdeleteButtonClickedです

もう一度ありがとうございます。

+1

DELETE FROM "+ TABLE_PRODUCTS +" WHERE "+ COLUMN_PRODUCTNAME +" = \ "" + productName + "\"; "'に余分な '' 'があるようです。 ?デバッグする最良の方法は、Where節なしで単純なDelete文を実行し、SQL文の問題かJavaコードの問題かを確認することです。 –

+0

db.execSQL( "DELETE FROM PRODUCTS(tablename)WHERE PRODUCTNAME =" "+ productName +" ' ");これは、データベースからアイテムを削除するための正しい削除クエリでした。 –

+0

" DELETE "を" SELECT "に置き換え、ログを観察することをお勧めします。 –

答えて

0

は、 "="、

WRONG = db.execSQL( "DELETE FROM" + TABLE_PRODUCTS + "WHERE" + COLUMN_PRODUCTNAME + "= \" "+商品名+" のために "\" を使用しないでください\ ";");

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

又は

db.delete( "TABLE_PRODUCTS"、COLUMN_PRODUCTNAME + "=" +商品名、NULL);

関連する問題