How to execute a SQLite query in an Android application?

I am trying to use this query in an Android database, but it does not return any data. Did I miss 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; 
+64
android sqlite android-sqlite
Aug 07 '09 at 6:24
source share
5 answers

This will return the cursor you need.

 Cursor cursor = db.query(TABLE_NAME, new String[] {"_id", "title", "title_raw"}, "title_raw like " + "'%Smith%'", null, null, null, null); 
+107
Aug 07 '09 at 7:15
source share

Alternatively there is db.rawQuery (sql, selectionArgs).

 Cursor c = db.rawQuery(select, null); 
+57
Aug 10 '09 at 16:24
source share

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 } 
+25
Mar 21 2018-12-12T00:
source share

I came here to remind you how to configure the request, but the existing examples were not easy. Here is an example with a lot of 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); 

Options

  • table : the name of the table you want to query
  • columns : the names of the columns you want to return. Do not return data that you do not need.
  • selection : the data of the row you want to return from the columns (this is a WHERE clause.)
  • selectionArgs : is this being replaced by ? in the selection String above.
  • groupBy and having : these groups duplicate data in a column with data that has certain conditions. Any unnecessary parameters can be set to zero.
  • orderBy : sort data
  • limit : limit the number of returned results
+7
Oct 24 '16 at 4:28
source share

Try this, this works for my code name - line:

 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); 
+1
Apr 14 '14 at 2:02
source share



All Articles