android - 如何通过仅输入特定列的字符串值来删除 SQLite 数据库的整行?

标签 android sqlite android-sqlite

我正在使用这个方法来查询这个字符串:

public void deletedata(){
    p=srt.split(",");

    DatabaseHandler dba=new DatabaseHandler(this);
    for(String s:p) {
        dba.removeSingleproduct(s);
    }

数据库方法是:

public boolean removeSingleproduct(String name) {
        SQLiteDatabase db = this.getWritableDatabase();

        return db.delete(tablename, productinserted + "=" + name, null) > 0;
    }

我想通过调用数据库只删除一行,因为插入的产品可以有两个相同的值。

请大家帮忙

最佳答案

Since you're deleting with a selectedValue String, add a single quote before and after the name

return db.delete(tablename, productinserted + " = '" + name + "'", null) > 0;

Or you can simplify your code.

public int removeSingleproduct(String name) {
    return getWritableDatabase().delete(tablename, productinserted + " = ?", new String[] { name });
}

Return int - the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause.

关于android - 如何通过仅输入特定列的字符串值来删除 SQLite 数据库的整行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46614036/

相关文章:

android - sql的haversine公式定义

Android Studio-程序类型已经存在 : com. google.android.gms.internal.measurement.zzwp

安卓相机预览人像比例

c# - Xamarin c# CSV 正在 Android 目录中创建但未写入

java - maven RESTful Web 服务项目中的 SQLite

android - 如何使用游标适配器从 SQLite 数据库创建 ListView

android - 使用 Samsuns Galaxy S3 单击全屏 View 时 Espresso 抛出错误

带有 AsyncTask 的 Android 自定义 CursorAdapter

ios - 无法打开文件,因为在尝试打开 SQLite3 数据库时无法确定其内容的文本编码

android - 将 SparseBooleanArray 保存到 SharedPreferences