在Android中删除SQLite中的行

这可能是一个愚蠢的问题,但我是SQLite新手,我似乎无法弄清楚这一点。 我有一个表有KEY_ROWIDKEY_NAMEKAY_LATITUDEKEY_LONGITUDE 。 我希望用户能够select一个并删除它; 任何人都可以给我一个方向,开始? 我的问题是在实际删除只给出其名称的行。

相关代码:

 public class BeaconDatabase { public static final String KEY_ROWID = "_id"; public static final String KEY_NAME = "beacon_name"; public static final String KEY_LATITUDE = "beacon_lat"; public static final String KEY_LONGITUDE = "beacon_lon"; private static final String DATABASE_NAME ="BeaconDatabase"; private static final String DATABASE_TABLE ="beaconTable"; private static final int DATABASE_VERSION = 1; private DbHelper helper; private final Context context; private SQLiteDatabase db; public BeaconDatabase(Context context) { this.context = context; } public BeaconDatabase open() { helper = new DbHelper(this.context); db = helper.getWritableDatabase(); return this; } public void close() { helper.close(); } public long createEntry(String name, Double lat, Double lon) { ContentValues cv = new ContentValues(); cv.put(KEY_NAME, name); cv.put(KEY_LATITUDE, lat); cv.put(KEY_LONGITUDE, lon); return db.insert(DATABASE_TABLE, null, cv); } public void deleteEntry(long row) { // Deletes a row given its rowId, but I want to be able to pass // in the name of the KEY_NAME and have it delete that row. //db.delete(DATABASE_TABLE, KEY_ROWID + "=" + row, null); } public String getData() { String[] columns = { KEY_ROWID, KEY_NAME, KEY_LATITUDE, KEY_LONGITUDE }; Cursor cursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null); String result = ""; int iRow = cursor.getColumnIndex(KEY_ROWID); int iName = cursor.getColumnIndex(KEY_NAME); int iLat = cursor.getColumnIndex(KEY_LATITUDE); int iLon = cursor.getColumnIndex(KEY_LONGITUDE); for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { result += cursor.getString(iRow) + ": " + cursor.getString(iName) + " - " + cursor.getDouble(iLat) + " latitude " + cursor.getDouble(iLon) + " longitude\n"; } return result; } private static class DbHelper extends SQLiteOpenHelper { public DbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT NOT NULL, " + KEY_LATITUDE + " DOUBLE, " + KEY_LONGITUDE + " DOUBLE);" ); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); onCreate(db); } } } 

你可以这样尝试:

  //---deletes a particular title--- public boolean deleteTitle(String name) { return db.delete(DATABASE_TABLE, KEY_NAME + "=" + name, null) > 0; } 

试试这样,你可能会得到你的解决scheme

 String table = "beaconTable"; String whereClause = "_id=?"; String[] whereArgs = new String[] { String.valueOf(row) }; db.delete(table, whereClause, whereArgs); 

最好也使用whereargs;

 db.delete("tablename","id=? and name=?",new String[]{"1","jack"}); 

这就像使用这个命令:

 delete from tablename where id='1' and name ='jack' 

并以这种方式使用删除function是好的,因为它消除了SQL注入。

直到我明白你的问题,你想把两个条件select一个行被删除。为此,你需要做的:

 public void deleteEntry(long row,String key_name) { db.delete(DATABASE_TABLE, KEY_ROWID + "=" + row + " and " + KEY_NAME + "=" + key_name, null); /*if you just have key_name to select a row,you can ignore passing rowid(here-row) and use: db.delete(DATABASE_TABLE, KEY_NAME + "=" + key_name, null); */ } 

试试这个代码

 public void deleteRow(String value) { SQLiteDatabase db = this.getWritableDatabase(); db.execSQL("DELETE FROM " + TABLE_NAME+ " WHERE "+COlUMN_NAME+"='"+value+"'"); db.close(); } 

试试这个代码…

 private static final String mname = "'USERNAME'"; public void deleteContact() { db.delete(TABLE_CONTACTS, KEY_NAME + "=" + mname, null); } 

这完美地工作:

 public boolean deleteSingleRow(String rowId) { return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; } 

你也可以参考这个例子 。

伙计们,这是一个通用的方法,你可以使用所有的表,完全在我的情况下工作。

 public void deleteRowFromTable(String tableName, String columnName, String keyValue) { String whereClause = columnName + "=?"; String[] whereArgs = new String[]{String.valueOf(keyValue)}; yourDatabase.delete(tableName, whereClause, whereArgs); } 

如果上面的解决scheme不适合你,那么试试这个也是因为它为我工作。

 public boolean deleteRow(String name) { return db.delete(DATABASE_TABLE, KEY_NAME + "='" + name +"' ;", null) > 0; } 

如果您正在使用SQLiteDatabase,那么有一个删除方法

删除的定义

 int delete (String table, String whereClause, String[] whereArgs) 

示例实现

现在我们可以写一个名为delete的方法

 public void delete(String value) { db.delete(DATABASE_TABLE, KEY_NAME + "=?", new String[]{String.valueOf(value)}); } 

如果你想删除所有logging,那么只需将null传递给上面的方法,

 public void delete() { db.delete(DATABASE_TABLE, null, null); } 

信息来源

试试这个:

 public void deleteEntry(long rowId) { database.delete(DATABASE_TABLE , KEY_ROWID + " = " + rowId, null);} 
 public boolean deleteRow(long l) { String where = "ID" + "=" + l; return db.delete(TABLE_COUNTRY, where, null) != 0; } 

你可以做这样的事情,分享我的工作代码片段

确保查询是这样的

DELETE FROM tableName WHERE KEY__NAME ='parameterToMatch'

 public void removeSingleFeedback(InputFeedback itemToDelete) { //Open the database SQLiteDatabase database = this.getWritableDatabase(); //Execute sql query to remove from database //NOTE: When removing by String in SQL, value must be enclosed with '' database.execSQL("DELETE FROM " + TABLE_FEEDBACKS + " WHERE " + KEY_CUSTMER_NAME + "= '" + itemToDelete.getStrCustName() + "'" + " AND " + KEY_DESIGNATION + "= '" + itemToDelete.getStrCustDesignation() + "'" + " AND " + KEY_EMAIL + "= '" + itemToDelete.getStrCustEmail() + "'" + " AND " + KEY_CONTACT_NO + "= '" + itemToDelete.getStrCustContactNo() + "'" + " AND " + KEY_MOBILE_NO + "= '" + itemToDelete.getStrCustMobile() + "'" + " AND " + KEY_CLUSTER_NAME + "= '" + itemToDelete.getStrClusterName() + "'" + " AND " + KEY_PRODUCT_NAME + "= '" + itemToDelete.getStrProductName() + "'" + " AND " + KEY_INSTALL_VERSION + "= '" + itemToDelete.getStrInstalledVersion() + "'" + " AND " + KEY_REQUIREMENTS + "= '" + itemToDelete.getStrRequirements() + "'" + " AND " + KEY_CHALLENGES + "= '" + itemToDelete.getStrChallenges() + "'" + " AND " + KEY_EXPANSION + "= '" + itemToDelete.getStrFutureExpansion() + "'" + " AND " + KEY_COMMENTS + "= '" + itemToDelete.getStrComments() + "'" ); //Close the database database.close(); }