【问题标题】:How to perform an SQLite query within an Android application?如何在 Android 应用程序中执行 SQLite 查询?
【发布时间】:2010-11-17 14:38:43
【问题描述】:

我正在尝试在我的 Android 数据库上使用此查询,但它不返回任何数据。我错过了什么吗?

SQLiteDatabase db = mDbHelper.getReadableDatabase();
    String select = "Select _id, title, title_raw from search Where(title_raw like " + "'%Smith%'" +
    ")";        
    Cursor cursor = db.query(TABLE_NAME, FROM, 
            select, null, null, null, null);
    startManagingCursor(cursor);
    return cursor;

【问题讨论】:

标签: android sqlite android-sqlite


【解决方案1】:

这将为您返回所需的光标

Cursor cursor = db.query(TABLE_NAME, new String[] {"_id", "title", "title_raw"}, 
                "title_raw like " + "'%Smith%'", null, null, null, null);

【讨论】:

  • 如果您希望where 条件为“一切”,您是否只传递null
  • @Matt:是的,只需将 null 传递给 no where caluse
  • 为了改进缓存,您可以像这样分开选择和选择参数:..., "title_raw like ?", new String[]{"'%Smith%'"}, ...。来源:android documentation.
  • @sulai SQLiteDatabase 和 ContentResolver 的连接在哪里? SQLite 的链接是:developer.android.com/reference/android/database/sqlite/… 他们没有说任何关于缓存的内容。我认为应该使用单独的参数来对抗 sql 注入。否则,我认为它们不是很重要。
【解决方案2】:

另外,db.rawQuery(sql, selectionArgs) 存在。

Cursor c = db.rawQuery(select, null);

【讨论】:

    【解决方案3】:

    如果您要匹配的模式是变量,这也将起作用。

    dbh = new DbHelper(this);
    SQLiteDatabase db = dbh.getWritableDatabase();
    
    Cursor c = db.query(
        "TableName", 
        new String[]{"ColumnName"}, 
        "ColumnName LIKE ?", 
        new String[]{_data+"%"}, 
        null, 
        null, 
        null
    );
    
    while(c.moveToNext()){
        // your calculation goes here
    }
    

    【讨论】:

      【解决方案4】:

      我来这里是为了提醒如何设置查询,但现有示例很难理解。这是一个有更多解释的示例。

      SQLiteDatabase db = helper.getReadableDatabase();
      
      String table = "table2";
      String[] columns = {"column1", "column3"};
      String selection = "column3 =?";
      String[] selectionArgs = {"apple"};
      String groupBy = null;
      String having = null;
      String orderBy = "column3 DESC";
      String limit = "10";
      
      Cursor cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
      

      参数

      • table:你要查询的表名
      • columns:要返回的列名。不要返回您不需要的数据。
      • selection:你希望从列中返回的行数据(这是 WHERE 子句。)
      • selectionArgs:替换上面selection 字符串中的?
      • groupByhaving:这会将重复数据分组到具有特定条件的数据的列中。任何不需要的参数都可以设置为 null。
      • orderBy:对数据进行排序
      • limit:限制返回结果的数量

      【讨论】:

        【解决方案5】:

        试试这个,这适用于我的代码 name 是一个字符串:

        cursor = rdb.query(true, TABLE_PROFILE, new String[] { ID,
            REMOTEID, FIRSTNAME, LASTNAME, EMAIL, GENDER, AGE, DOB,
            ROLEID, NATIONALID, URL, IMAGEURL },                    
            LASTNAME + " like ?", new String[]{ name+"%" }, null, null, null, null);
        

        【讨论】:

          猜你喜欢
          • 2015-11-27
          • 2011-08-28
          • 1970-01-01
          • 2013-06-23
          • 1970-01-01
          • 2015-07-04
          • 2012-05-18
          • 2014-12-28
          • 2022-12-06
          相关资源
          最近更新 更多