68

我正在尝试在我的 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;
4

5 回答 5

112

这将为您返回所需的光标

Cursor cursor = db.query(TABLE_NAME, new String[] {"_id", "title", "title_raw"}, 
                "title_raw like " + "'%Smith%'", null, null, null, null);
于 2009-08-07T07:15:18.070 回答
57

或者, db.rawQuery(sql, selectionArgs) 存在。

Cursor c = db.rawQuery(select, null);
于 2009-08-10T16:24:36.410 回答
27

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

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
}
于 2012-03-21T14:19:45.807 回答
13

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

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:这会将重复数据分组到具有特定条件的数据的列中。任何不需要的参数都可以设置为空。
  • orderBy: 对数据进行排序
  • limit: 限制返回结果的数量
于 2016-10-24T04:28:14.840 回答
1

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

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);
于 2014-04-14T02:02:42.013 回答