我是SQLite数据库和Android应用程序的新手。我想在用户输入名称并单击“删除帐户”按钮时删除特定记录。
我有以下代码,但我似乎无法实现我想要的。您对我的代码可能出现的问题有任何见解吗?
mydbadapter.java
中
private static final String DATABASE_NAME = "customer.db";
private static final String DATABASE_TABLE = "customerAcc";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase _db;
private final Context context;
public static final String name = "_name";
public static final int COLUMN_NAME_ID = 0;
public static final String password = "_password";
public static final int COLUMN_PW_ID = 1;
public static final String address = "_address";
public static final int COLUMN_ADDRESS_ID = 2;
public static final String phoneNumber = "_phoneNumber";
public static final int COLUMN_PHONE_ID = 3;
protected static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + " " + "(" + name + " Text not null, " + password + " Text not null, "
+ address + " Text not null, " + phoneNumber + " text not null);";
private String MYDBADAPTER_LOG_CAT = "MY_LOG";
private MyDBOpenHelper dbHelper;
public MyDbAdapter(Context _context)
{
this.context = _context;
dbHelper = new MyDBOpenHelper (context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void close()
{
_db.close();
Log.w(MYDBADAPTER_LOG_CAT, "DB Closed");
}
public void open() throws SQLiteException
{
try
{
_db = dbHelper.getWritableDatabase();
Log.w(MYDBADAPTER_LOG_CAT, "DB opened as writable database");
}
catch (SQLiteException e)
{
_db = dbHelper.getReadableDatabase();
Log.w(MYDBADAPTER_LOG_CAT, "DB opened as readable database");
}
}
public long insertEntry(String cname, String cpassword, String caddress, String cphone)
{
// create codes go here
}
public boolean removeEntry(String custname)
{
try
{
_db = dbHelper.getWritableDatabase();
return _db.delete(DATABASE_TABLE, name + "=" + custname, null)>0;
}
catch (SQLiteException e)
{
return false;
}
}
public boolean updateEntry(long _rowIndex, String entryName, String entryTel)
{
//update codes are here
}
public Cursor retrieveAllEntriesCursor()
{
//retrieve codes are here
}
public class MyDBOpenHelper extends SQLiteOpenHelper
{
public MyDBOpenHelper(Context context, String name, CursorFactory factory, int version)
{
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
Log.w(MYDBADAPTER_LOG_CAT, "Helper : DB " + DATABASE_TABLE + " Created!!");
}
} // End of myDBOpenHelper
}// End of myDBAdapter
MainActivity.java
中
btnRemove.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String custname = etRName.getText().toString();
myDB.removeEntry(custname);
}
});
我的代码没有给我任何错误,但是当我点击“删除帐户”按钮时,记录仍然存在。
答案 0 :(得分:1)
虽然我会推荐
,但是有效db.delete(TABLE, "column_name=?", new String[] { String.valueOf(custname) });
或使用
String query = "DELETE FROM " +TABLE_NAME+ " WHERE " + COLUM_NAME+ " = " + "'"+VALUE +"'" ;
db.execSQL(query);
答案 1 :(得分:0)
public boolean removeEntry(String custname) {
bolean returnBoolean;
试 {
db.delete(TABLE_NAME, "column_name=?", new String[]{String.valueOf(custname)});
returnBoolean= true;
}
catch(Exception e)
{
returnBoolean= false;
e.printStackTrace();
}
finally
{
db.close();
}
return returnBoolean; }
答案 2 :(得分:0)
您的查询语法似乎有些问题。这是我的代码中的一行,它适用于我
db.delete(Table_name, KEY_COLUMN + "=?", new String[] {String.valueOf(custname)});
你没有获得查询结构。第二个参数用于查询的 where 子句。请参阅THIS帖子的第一个答案,以便更好地理解语法。
答案 3 :(得分:0)
我建议你看看cupboard,这里的代码会大大减少/简化,这使得这种工作更具可读性/可理解性:)