Java 没有这样的列:KEY_PUBNAME
我正在制作一个将数据存储在SQLite数据库中的应用程序。我希望能够添加、编辑和删除此数据库中的数据。我可以应用到它没有问题。我目前正在尝试从数据库中删除,但我一直收到相同的错误 1没有这样的列:KEY\u PUBNAME 这是我的Java文件:Java 没有这样的列:KEY_PUBNAME,java,android,sqlite,Java,Android,Sqlite,我正在制作一个将数据存储在SQLite数据库中的应用程序。我希望能够添加、编辑和删除此数据库中的数据。我可以应用到它没有问题。我目前正在尝试从数据库中删除,但我一直收到相同的错误 1没有这样的列:KEY\u PUBNAME 这是我的Java文件: package com.example.beer_budget3; import android.app.Activity; import android.os.Bundle; import android.view.View; import an
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>
密钥PUBNAME在数据库适配器中明确声明。
任何帮助都很好。如果您看到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;
}
您提到的keyname是KEY_PUBNAME。但是,在创建表的地方,您定义的键名是Pub_Name。在这里:
public static final String KEY_PUBNAME = "Pub_Name";
这就是为什么没有找到它。KEY_PUBNAME是您声明的变量,而不是列的名称。您可能希望尝试在db.delete语句中更改它
public static final String KEY_PUBNAME = "Pub_Name";