2016-03-25 2 views
1

Ich bin ein Anfänger für Android App, und lernen Sie es von Bucky Tutorial. Für die Datenbank, ich denke, ich habe den gleichen Code mit dem Tutorial online, aber Add-Funktion funktioniert perfekt, aber die Funktion Löschen funktioniert nicht.Android App Datenbank Löschen Funktion funktioniert nicht, aber Funktion Hinzufügen funktioniert

Fehler:

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(""); 
    } 


} 

Produkte Klasse:

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

Jemand bitte helfen Sie mir. Ich weiß wirklich nicht, wo der Bug ist. Danke! AddButtonClicked funktioniert einwandfrei. Das einzige Problem ist deleteButtonClicked

Danke nochmal.

+1

Sieht so aus, als gäbe es ein zusätzliches '' 'in Ihrer Delete-Anweisung' "DELETE FROM" + TABLE_PRODUCTS + "WHERE" + COLUMN_PRODUCTNAME + "= \" "+ productName +" \ ";". Können Sie das bitte bestätigen? ? Die beste Möglichkeit zum Debuggen ist, eine einfache Delete-Anweisung ohne WHERE-Klausel auszuführen und festzustellen, ob es sich um ein SQL-Anweisungsproblem oder ein Java-Code-Problem handelt. –

+0

db.execSQL ("DELETE FROM PRODUCTS (Tabellenname) WHERE PRODUCTNAME = '" + productName +' ' "); das war eine richtige Löschabfrage für das Löschen des Elements aus der Datenbank. –

+0

Ich würde vorschlagen," DELETE "durch" SELECT "zu ersetzen und Protokoll zu beachten. –

Antwort

0

Verwenden Sie "\" nicht für "="

FALSCH = db.execSQL ("DELETE FROM" + TABLE_PRODUCTS + "WHERE" + COLUMN_PRODUCTNAME + "= \" "+ product +" \ ";");

KORREKT = db.execSQL ("DELETE FROM" + TABELLE_PRODUKTE + "WHERE" + COLUMN_PRODUCTNAME + "=" + productName);

oder

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

Verwandte Themen