0

我有一个预编译的数据库,我想根据传递的参数获取数据

但由于某种奇怪的原因,光标结果总是空的

我在适配器中的 getData 方法是

    public Cursor getData(String uso, String eta, String category,
            String class) {
        try {
            Cursor c;
            if (class.equals(null) && category.equals(null)) {
                c = mDb.rawQuery("SELECT * FROM dbtable WHERE Uso LIKE \""
                        + uso + "\" AND Eta LIKE \"" + eta
                        + "\" AND Language LIKE \"" + language
                        + "\" order by Name asc", null);
            } else if (class.equals(null)) {
                c = mDb.rawQuery("SELECT * FROM dbtable WHERE Uso LIKE \""
                        + uso + "\" AND Eta LIKE \"" + eta
                        + "\" AND Tipo LIKE \"" + category
                        + "\" AND Language LIKE \"" + language
                        + "\" order by Name asc", null);
            } else if (category.equals(null)) {
                c = mDb.rawQuery("SELECT * FROM dbtable WHERE Uso LIKE \""
                        + uso + "\" AND Eta LIKE \"" + eta
                        + "\" AND Certification LIKE \"" + class
                        + "\" AND Language LIKE \"" + language
                        + "\" order by Name asc", null);
            } else {
                c = mDb.rawQuery("SELECT * FROM dbtable WHERE Uso LIKE \""
                        + uso + "\" AND Eta LIKE \"" + eta
                        + "\" AND Tipo LIKE \"" + category
                        + "\" AND Certification LIKE \"" + class
                        + "\" AND Language LIKE \"" + language
                        + "\" order by Name asc", null);
            }
            if (c != null) {
                c.moveToNext();
            }
            return c;
        } catch (SQLException mSQLException) {
            Log.e(TAG, "getDataCar >>" + mSQLException.toString());
            throw mSQLException;
        }

    } 
...

此游标用于将所有条目放入 Car 对象数组的方法中

public Car[] getDataDBCarsArray(String uso, String eta,
            String category, String class) {
        ArrayList<Car> list = new ArrayList<Car>();
        Cursor c = getData(uso, eta, category, class);
        try {
.....

一切正常如果不是上面的 getData(....) 我尝试使用

public Cursor getAllData(){
    try {
        Cursor c = mDb.rawQuery("SELECT * FROM dbtable order by Name asc", null);
        if (c != null) {
            c.moveToNext();
        }
        return c;
    }catch (SQLException mSQLException) {
        throw mSQLException;
    }

}

所以 getData(...) 中的查询似乎有问题。

但我无法弄清楚错误在哪里。

如果我尝试打开数据库,则传递的参数有很多元素,但奇怪的是什么也没找到。

注意

language是取决于设备语言的静态值

4

1 回答 1

2

您忘记了 SQL 通配符...

这个:

WHERE column = "John"

是相同的:

WHERE column LIKE "John"

您想要合并%_通配符来创建一个子句,例如:

WHERE column LIKE "%John%"

此外,您应该利用 SQLite 参数化并保护自己免受SQL 注入攻击:

c = mDb.rawQuery("SELECT * FROM dbtable " + 
        "WHERE Uso LIKE ? AND Eta LIKE ? AND Language LIKE ? ORDER BY Name",
        new String[] { "%" + uso + "%", 
                       "%" + eta + "%", 
                       "%" + language + "%" });
于 2012-12-10T20:09:44.747 回答