【问题标题】:Android filter sqlite results for listviewAndroid过滤列表视图的sqlite结果
【发布时间】:2015-02-02 21:54:39
【问题描述】:

您好,我对在 Android 上显示 sqlite 结果的列表视图过滤结果的查询字符串的语法有问题。

  public Cursor searchByInputText(String inputText) throws SQLException {
      String query = "SELECT _id as _id," +" title" +  " from " + TABLE_NAME + " where " + "title" + " LIKE '" + inputText + "';";
      //String query = "SELECT _id as _id," +" title" +  " from " + TABLE_NAME + " where " + "title" + " LIKE '" + inputText + "';";
      //String query =  "SELECT _id from " + TABLE_NAME + " WHERE title="+inputText;
      Cursor mCursor = database.rawQuery("SELECT _id as _id, title from "+TABLE_NAME +" where title like " + inputText,null);

    if (mCursor != null) {
        mCursor.moveToFirst();
        System.out.println(query);
    }
    return mCursor;
}

我尝试了很多解决方案,但似乎没有一个适合我,任何建议都会很棒..谢谢

【问题讨论】:

    标签: java android sqlite filtering


    【解决方案1】:

    当您使用String 时,输入where 子句,它应该用单引号括起来。

    where title like '%" + inputText +"'"
    

    【讨论】:

    • 感谢您的快速响应,阻止了崩溃,但列表视图中没有显示任何结果。
    • @n4zg:这是不同的问题。此答案仅有助于如何修复您的 SQL。请确保您首先从 SQLite 获取数据。完成此步骤后,请检查未将其设置为 listview 的原因。参考一些教程。
    • 我可以在列表视图中看到 sqlite 数据,但我需要过滤结果,我已经测试了查询而没有添加搜索参数,我可以看到结果.. 这个查询现在正确吗然后 Cursor mCursor = database.rawQuery("SELECT _id as _id, title from "+TABLE_NAME +" where title like '" + inputText +"'",null); ?
    • 我看到你在查询中使用了like,如果你使用like,你也应该添加 % 符号。请参阅更新的查询。在此处查看有关类似语法的更多详细信息techonthenet.com/sql/like.php
    • 我得到了以下关于 android.database.sqlite.SQLiteException: near "%": syntax error (code 1): , while compile: SELECT _id as _id, title from allfeed where title like %'f*'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2013-06-29
    相关资源
    最近更新 更多