2014-11-24 156 views
-1

我正在製作一個應用程序,它將數據存儲在SQLite數據庫中。我希望能夠添加,編輯和刪除數據庫中的數據。我可以應用它沒有問題。我目前正在嘗試從數據庫中刪除,但我一直得到相同的錯誤。沒有這樣的列:KEY_PUBNAME

(1)沒有這樣的列:KEY_PUBNAME

這是我的Java文件:

package com.example.beer_budget3; 


import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 
import android.content.Intent; 

//Need to update delete layout after deleting row 
public class Delete extends Activity 
{ 
    //Creating an object name for my database 
    DatabaseSetup2 db = new DatabaseSetup2(this); 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     //This page layout is located in the delete XML file 
     setContentView(R.layout.delete);//Put one of these in each class 

     //Delete button that has been created in the delete XML file 
     Button delete = (Button)findViewById(R.id.deletepub); 
     delete.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       //This page links back to the MainMenu page 
       Intent i = new Intent(Delete.this, MainMenu.class); 
       //Calling the deleting function 
       deleting(v); 
       //Activating the intent 
       startActivity(i); 
      } 
     }); 
    } 


    public void deleting(View v) 
    { 
     db.open(); 
     //Save user input into rowId 
     EditText pnametxt = (EditText)findViewById(R.id.delete1); 
     //Open the database 

     String pname2 = pnametxt.getText().toString(); 

     db.deletePub(pname2); 

     db.close(); 
    } 
} 

這裏是我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/background" 
    tools:context="com.example.beer_budget3.delete" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="85dp" 
     android:layout_marginBottom="20dp" 
     android:text="@string/app_name" 
     android:textColor="@color/blue" 
     android:textStyle="bold" 
     android:textSize="30sp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/details" 
     android:layout_marginLeft="50dp" 
     android:layout_marginBottom="30dp" 
     android:textSize="25sp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/pub" 
     android:textSize="20sp"/> 

    <EditText 
     android:id="@+id/delete1" 
     android:inputType="text" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     /> 

    <Button 
     android:id="@+id/deletepub" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="300dp" 
     android:layout_marginLeft="130dp" 
     android:onClick="delete" 
     android:text="@string/delete" /> 


</LinearLayout> 

這裏是我的數據庫適配器:

package com.example.beer_budget3; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 
import android.widget.EditText; 

public class DatabaseSetup2 

{ 

    // These are the names of the columns the table will contain 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_PUBNAME = "Pub_Name"; 
    public static final String KEY_LOCATION = "Location"; 
    public static final String KEY_PRICE = "Price"; 

    private static final String DATABASE_NAME = "CillinsAssignment"; 
    private static final String DATABASE_TABLE = "Beer_Budget"; 
    private static final int DATABASE_VERSION = 1; 

    // This is the string containing the SQL database create statement 
    private static final String DATABASE_CREATE = "CREATE TABLE " + DATABASE_TABLE + 
      "(" +KEY_ROWID + " integer primary key autoincrement, "+KEY_PUBNAME +" text not 
    null, "+KEY_LOCATION+" text not null, "+KEY_PRICE+ " text not null);"; 

private final Context context; 

private DatabaseHelper DBHelper; 
// utility class that makes it easy to create and maintain an SQLLite database 
private SQLiteDatabase db;//Class containing methods to manage a local SQLLite Database file 

// constructor for your class 
public DatabaseSetup2(Context ctx) 
{ 
    // Context is a way that Android transfers info about Activities and apps. 
    this.context = ctx; 
    DBHelper = new DatabaseHelper(context); 
} 

// This is the helper class that will create the dB if it doesn’t exist and 
//upgrades it if the structure has changed. It needs a constructor, an 
//onCreate() method and an onUpgrade() method 

private static class DatabaseHelper extends SQLiteOpenHelper 
{ 
    // constructor for your dB helper class. This code is standard. You’ve set 
    //up the parameter values for the constructor already…database name,etc 
    DatabaseHelper(Context context) 
    { 
     super(context, DATABASE_NAME, null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) 
    { 
     // The 「Database_create」 string below needs to contain the SQL 
     //statement needed to create the dB 
     try 
     { 
      db.execSQL(DATABASE_CREATE);   
     } 
     catch (SQLException e) 
     { 
      e.printStackTrace();   
     } 

    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
     // If you want to change the structure of your database, e.g. 
     // Add a new column to a table, the code will go head.. 
     //This method only triggers if the database version number has 
     //increased 
     Log.w("test", "Upgrading database from version " + oldVersion + " to "      
     + newVersion + ", which will destroy all old data");    
     db.execSQL("DROP TABLE IF EXISTS Beer_Budget");   
     onCreate(db); 

    } 
}// end of the help class 

    // from here on, include whatever methods will be used to access or change data 
    //in the database 
    //---opens the database--- any activity that uses the dB will need to do this 
    public DatabaseSetup2 open() throws SQLException 
    { 
     db = DBHelper.getWritableDatabase(); 
     return this; 
    } 

    //---closes the database--- any activity that uses the dB will need to do this 
    public void close() 
    { 
     DBHelper.close(); 
    } 

    //---insert a pub into the database--- 
    public long insertPub(String Pub_Name, String Location, String Price) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_PUBNAME, Pub_Name); 
     initialValues.put(KEY_LOCATION, Location); 
     initialValues.put(KEY_PRICE, Price); 
     return db.insert(DATABASE_TABLE, null, initialValues); 
    } 

    //---deletes a particular pub--- 
    public boolean deletePub(String Pub_Name) 
    { 
     //delete statement. If any rows deleted (i.e. >0), returns true 
     return db.delete(DATABASE_TABLE, "KEY_PUBNAME = "+ Pub_Name+" ", null) > 0; 
    } 

    //---retrieves all the rows--- 
    public Cursor getAllPubs() 
    { 
    return db.query(DATABASE_TABLE, new String[] 
    { 
     KEY_ROWID, 
     KEY_PUBNAME, 
     KEY_LOCATION, 
     KEY_PRICE}, 
     null, 
     null, 
     null, 
     null, 
     null); 
    } 
    //---retrieves a particular row--- 
    public Cursor getPub(int _id) throws SQLException 
    { 
    Cursor mCursor = db.query(DATABASE_TABLE, new String[] 
    { 
     KEY_ROWID, 
     KEY_PUBNAME, 
     KEY_LOCATION, 
     KEY_PRICE 
    }, 
    KEY_ROWID + "=" + _id, 
    null, 
    null, 
    null, 
    null 
    ); 
    if (mCursor != null) { 
    mCursor.moveToFirst(); 
    } 
    return mCursor; 
    } 

} 

KEY_PUBNA ME在數據庫適配器中明確聲明。 任何幫助將是偉大的。

回答

2

如果你看到你的DatabaseHelper,您已經定義如下:

//---deletes a particular pub--- 
public boolean deletePub(String Pub_Name) 
{ 
    //delete statement. If any rows deleted (i.e. >0), returns true 
    return db.delete(DATABASE_TABLE, "KEY_PUBNAME = "+ Pub_Name+" ", null) > 0; 
} 

你提到的鍵名是KEY_PUBNAME。但是,在創建表的位置,您定義的鍵名是Pub_Name。在這裏:

public static final String KEY_PUBNAME = "Pub_Name"; 

這就是爲什麼找不到它。 KEY_PUBNAME是您聲明的變量,而不是列的名稱。您可能需要嘗試在db.delete聲明中更改它。

+0

根特。謝謝 – 2014-11-24 19:00:59