【问题标题】:How to search for string match in specific row of table in database in android如何在android中数据库的特定行中搜索字符串匹配
【发布时间】:2019-09-10 04:59:52
【问题描述】:

我正在构建一个应用程序并第一次处理数据库,我试图将数据 TEXT 作为输入并在特定行中搜索其匹配项。我没有这样做。

我已经选择了特定的行。我有六列,我根据 id 得到了特定的行,但是比较其他五列中的输入文本会给出有问题的输出。 根据我在下面代码中给出的方法,它在将其与第一列或最后一列条目进行比较后给出结果,但不是全部。

    public Cursor finddata(String name,int ida, int guess){
        String Query = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME1='" +name+ "'";
                    cursor=db.rawQuery(Query,null);
                    check=1;
                    if(cursor==null){
                        String Query2 = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME2='" +name+ "'";
                        cursor=db.rawQuery(Query2,null);
                        check=2;
                    }
                    if(cursor==null){
                        String Query3 = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME3='" +name+ "'";
                        cursor=db.rawQuery(Query3,null);
                        check=3;
                    }
                    if(cursor==null){
                        String Query4 = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME4='" +name+ "'";
                        cursor=db.rawQuery(Query4,null);
                        check=4;
                    }
                    if(cursor==null){
                        String Query5 = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME5='" +name+ "'";
                        cursor=db.rawQuery(Query5,null);
                        check=5;
                    }
                    if(cursor==null){
                        String Query6 = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME6='" +name+ "'";
                        cursor=db.rawQuery(Query6,null);
                    }
                    return cursor;
                    }

我认为我在光标上应用的 if 条件有一些错误 "if(cursor==null)" 因为它只是通过比较第一个语句而不是 if 来给出答案,即使它在第一个语句中是否匹配。

如果有一些直接查询语句可以做到这一点?????

我在互联网上搜索了很多,但找不到解决方案。 如果可以,请不要引导我回答已经回答的问题并回答这个问题。 问候

【问题讨论】:

  • 帮自己一个忙搬到房间图书馆:)
  • 我想通了。 . .如果我使用“if(cursor.getCount==0)”代替“if(cursor==null)”,它工作正常。 . .感谢您的建议

标签: java android sqlite android-sqlite cursor


【解决方案1】:

你做错了什么是你检查rawQuery()返回的Cursor对象是否是null
rawQuery()返回一个Cursor可能是空的(没有任何行)但它永远不可能是null
您可以通过检查 moveToFirst() 方法来检查 Cursor 中是否有任何行。
也永远不要使用 sql 语句的参数连接。不安全。
使用占位符?rawQuery() 的第二个参数来传递参数。
所以改成这样:

public Cursor finddata(String name, int ida, int guess){
    String sql = "SELECT NAME1 FROM Capital_Names " + 
        "WHERE ID = ? AND ? IN (NAME1, NAME2, NAME3, NAME4, NAME5)";
    return db.rawQuery(sql, new String[] {ida, name});
} 

您的代码中似乎没有使用finddata() 的参数guess,因此您可以将其删除。
您还确定只返回NAME1 列吗?您可以将其替换为 "SELECT * FROM..." 以返回所有列。

【讨论】:

  • 我现在没有将“guess”用于任何目的,但将来会在某个地方使用它,我想通了如果我使用“if(cursor.getCount==0)”代替“ if(cursor==null)" 它工作正常。 . .通过给出单语句命令,您使我的工作变得非常轻松,非常感谢。 . . . .
【解决方案2】:
public Cursor finddata(String name,int ida, int guess){
    String Query = "SELECT  NAME1 FROM Capital_Names 
                    WHERE ID = '" + ida + "'
                    AND
                   ( 
                   NAME1='" +name+ "' OR NAME2='" +name+ "' OR NAME3='" +name+ "' OR 
                   NAME4='" +name+ "' OR NAME5='" +name+ "')"
                   ;
return db.rawQuery(Query,null);
}

试试这个...

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 2011-11-12
    相关资源
    最近更新 更多