没有这样的列:KEY_PUBNAME

时间:2014-11-24 18:48:10

标签: java android sqlite

我正在创建一个将数据存储在SQLite数据库中的应用程序。我希望能够添加,编辑和删除此数据库中的数据。我可以应用它没问题。我目前正在尝试从数据库中删除但我一直收到同样的错误。

(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_PUBNAME在数据库适配器中明确声明。 任何帮助都会很棒。

1 个答案:

答案 0 :(得分: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语句中更改它。