Android 删除数据库表中给定一列值的行 - 这是一个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3314176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:40:54  来源:igfitidea点击:

Delete row in database table given one column value - which is a string

android

提问by Yasir Malang

I have a table with column headings: | _id (long) | Name (String) | x (integer) | y (integer) |

我有一个带有列标题的表格: | _id(长)| 名称(字符串)| x(整数)| y(整数)|

I want to delete the row in the table that has Name myName.

我想删除表中名称为 myName 的行。

// In onCreate
dbHelper = new DBAdapter(this);
dbHelper.open()

// Function in DBAdapter class
public boolean deleteTitleGivenName(String myName) 
{
    return dbHelper.delete(DATABASE_TABLE_2, KEY_NAME + "=" + myName, null) > 0;
}

// Function call in java code
dbHelper.deleteTitleGivenName(myName);  // this is where my code fails  

dbHelper.close();           

Just as a note: myName is for sure in the database. Also, I can not use the ROWID since I am getting myName from a ListView.

请注意:myName 肯定在数据库中。此外,我无法使用 ROWID,因为我是从 ListView 获取 myName。

I just started programming with Android and I have tried for days to solve this problem. Is my WHERE clause correct (KEY_NAME + "=" + myName)?

我刚开始用 Android 编程,我已经尝试了几天来解决这个问题。我的 WHERE 子句是否正确(KEY_NAME + "=" + myName)?

Thanks in advance.

提前致谢。

回答by EboMike

Sure, that works, although I would recommend

当然,这有效,虽然我会推荐

dbHelper.delete(DATABASE_TABLE_2, KEY_NAME + "=?", new String[] { myName })

for safety reasons. That way, you can have special characters without breaking your query. Did this not work?

出于安全原因。这样,您可以在不中断查询的情况下使用特殊字符。这不起作用吗?

回答by dacris

Try putting the value of the where clause in quotes...

尝试将 where 子句的值放在引号中...

KEY_NAME + "='" + myName + "'"

Also, it is bad practice (generally) to use a string-based WHERE clause. This is because if my name has an apostrophe in it, your app will not work. You should probably look for an alternative way to delete records from a database on Android.

此外,使用基于字符串的 WHERE 子句是不好的做法(通常)。这是因为如果我的名字中有撇号,您的应用程序将无法运行。您可能应该寻找一种替代方法来从 Android 上的数据库中删除记录。

回答by tony gil

depending on the complexity of your where condition, the "delete" method will not be the best solution.

根据您的 where 条件的复杂性,“删除”方法将不是最佳解决方案。

for example, if you want to delete all records whose "_id" is in a second table, like you would write in transactSQL: "...WHERE _id IN (SELECT _id FROM..." then the best solution might be to use the ".execSQL()" method directly on your database.

例如,如果您想删除“_id”在第二个表中的所有记录,就像您在 transactSQL 中写的那样:“...WHERE _id IN (SELECT _id FROM...”) 那么最好的解决方案可能是使用直接在您的数据库上使用“.execSQL()”方法。

myDatabase.execSQL("DELETE FROM myTable WHERE _id IN (SELECT _id FROM myOtherTable)");

or you could go real ugly and do something like:

或者你可以变得非常丑陋并执行以下操作:

int cursorRows = cursor.getCount();
String[] id2String = new String[cursorRows];
String id2StringUnique = "";
//for (int i=0;i<cursorRows;i++) {
int i=0;
for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){    
    id2String[i] = String.valueOf(cursor.getLong(index_ID));
    if (i==0) {
        id2StringUnique = "'"+id2String[i]+"'";
    } else {
        id2StringUnique += ",'"+id2String[i]+"'";
    }
    i++;
}
dbHelper.delete(DATABASE_TABLE_2, "_id IN (", id2StringUnique +")");

depending on number of items, you might have a problem with the length / size of your argument - besides it being diselegant to the extreme.

根据项目的数量,您的论点的长度/大小可能有问题-除了极端不雅之外。

回答by Durgpal Singh

just try this simple code

试试这个简单的代码

private void deleteItem()
 {
    try 
      {
      SQLiteDatabase db = mOpenHelper.getWritableDatabase();
      db.delete(TABLE_NAME, " title = 'haiyang'", null);
      setTitle("title");
    } catch (SQLException e) {

    }