How to perform an SQLite query within an Android application?

AndroidSqliteAndroid Sqlite

Android Problem Overview


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;

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

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

Solution 3 - Android

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
}

Solution 4 - Android

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

Solution 5 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDennieView Question on Stackoverflow
Solution 1 - AndroidPrashastView Answer on Stackoverflow
Solution 2 - AndroidxiojasonView Answer on Stackoverflow
Solution 3 - AndroidGautam MandsorwaleView Answer on Stackoverflow
Solution 4 - AndroidSuragchView Answer on Stackoverflow
Solution 5 - AndroidstylingView Answer on Stackoverflow