70

I am trying to use this query upon my Android database, but it does not return any data. Am I missing something?

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;
Swathin
  • 516
  • 1
  • 8
  • 23
Dennie
  • 2,621
  • 13
  • 42
  • 41

5 Answers5

112

This will return you the required cursor

Cursor cursor = db.query(TABLE_NAME, new String[] {"_id", "title", "title_raw"}, 
                "title_raw like " + "'%Smith%'", null, null, null, null);
Prashast
  • 5,645
  • 3
  • 30
  • 31
  • 3
    Do you just pass `null` if you want the `where` condition to be "everything"? – Matt Huggins Sep 21 '10 at 01:08
  • 2
    @Matt: Yes, simply pass null for no where caluse – Noah Nov 29 '12 at 14:25
  • To improve caching, you can separate selection and selection-arguments like this: `..., "title_raw like ?", new String[]{"'%Smith%'"}, ...`. Source: [android documentation](http://developer.android.com/reference/android/content/ContentResolver.html#query(android.net.Uri,%20java.lang.String[],%20java.lang.String,%20java.lang.String[],%20java.lang.String)). – sulai Jun 26 '13 at 08:43
  • @sulai Where is the connection between SQLiteDatabase and ContentResolver? The link to SQLite is: https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase There they don't say anything about caching. I think separate arguments should be used against sql injection. Otherwise they're not very important, I think. – The incredible Jan Jun 19 '18 at 10:54
58

Alternatively, db.rawQuery(sql, selectionArgs) exists.

Cursor c = db.rawQuery(select, null);
xiojason
  • 589
  • 3
  • 2
27

This will also work if the pattern you want to match is a variable.

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
}
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
Gautam Mandsorwale
  • 1,580
  • 1
  • 18
  • 27
14

I came here for a reminder of how to set up the query but the existing examples were hard to follow. Here is an example with more explanation.

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);

Parameters

  • table: the name of the table you want to query
  • columns: the column names that you want returned. Don't return data that you don't need.
  • selection: the row data that you want returned from the columns (This is the WHERE clause.)
  • selectionArgs: This is substituted for the ? in the selection String above.
  • groupBy and having: This groups duplicate data in a column with data having certain conditions. Any unneeded parameters can be set to null.
  • orderBy: sort the data
  • limit: limit the number of results to return
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
1

Try this, this works for my code name is a String:

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);
styling
  • 29
  • 6