我正在制作一个应用程序,这个应用程序的一个功能是从数据库中删除一个条目,我使用条目的rowid正确工作,但现在我希望用户能够使用一个删除行列名。我现在的代码是我认为会起作用的,但它给了我一个错误,有人可以帮忙吗?
这是我正在使用的删除类:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.pinchtapzoom.R;
public class Delete extends Activity implements View.OnClickListener{
public EditText item;
Button delete;
//instansiation the DBAdapter class to db, to call functions from DBApater use db.(function name)
DBAdapter db = new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete);
item = (EditText)findViewById(R.id.edit1);
delete = (Button)findViewById(R.id.delete);
delete.setOnClickListener(this);
}
public void onClick(View v) {
String deleteItem = item.getText().toString();
//if the ROW ID its a number (long)
try{
//long rowID = Long.parseLong(userInput);
//call the delete method
db.open();
db.deleteContact(deleteItem);
db.close();
Toast.makeText(Delete.this,"Item Deleted", Toast.LENGTH_SHORT).show();
}catch(NumberFormatException e){
Log.e("INPUT ERROR","Input is not a number!",e);
Toast.makeText(this,"Invalid Value!",Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.delete, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//returns to home screen, set on the button in xml
public void viewAssignments(View v)
{
Intent i = new Intent(this, MyActivity.class);
startActivity(i);
}
}
这是我在数据库类中调用的函数
//---deletes a particular record---
public boolean deleteContact(String item)
{
return db.delete(DATABASE_TABLE, KEY_ITEM + "=" + item, null) > 0;
}
我运行时的logcat错误是
Process: com.example.rory.dbtest, PID: 8165
android.database.sqlite.SQLiteException: no such column: shower (code 1): , while compiling: DELETE FROM assignments WHERE item=shower
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1494)
at com.example.rory.dbtest.DBAdapter.deleteContact(DBAdapter.java:91)
at com.example.rory.dbtest.Delete.onClick(Delete.java:48)
答案 0 :(得分:0)
试试这个
return db.delete(DATABASE_TABLE, KEY_ITEM + "=\"" + item + "\"", null) > 0;