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

2023-09-12 07:49:57 作者:仗剑走天涯

我和列标题的表:| _id(长)|名称(字符串)| X(整数)| Y(整数)|

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

我要删除在具有名称MYNAME表中的行。

I want to delete the row in the table that has Name 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();           

就像一张纸条:MYNAME可以肯定的是在数据库中。另外,我无法使用ROWID,因为我是从一个ListView越来越MYNAME。

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.

我刚开始与Android编程和我已经试过几天来解决这个问题。 难道我的WHERE子句正确(KEY_NAME +=+ 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)?

在此先感谢。

推荐答案

当然,这样的作品,但我会建议

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?