I want to use select query for retrieving data from table. I have found, rawQuery(query, selectionArgs)
method of SQLiteDatabase
class to retrieve data. But I don't know how the query
and selectionArgs
should be passed to rawQuery
method?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
rawQuery("SELECT id, name FROM people WHERE name = ? AND id = ?", new String[] {"David", "2"});
You pass a string array with an equal number of elements as you have "?"
回答2:
Maybe this can help you
Cursor c = db.rawQuery("query",null); int id[] = new int[c.getCount()]; int i = 0; if (c.getCount() > 0) { c.moveToFirst(); do { id[i] = c.getInt(c.getColumnIndex("field_name")); i++; } while (c.moveToNext()); c.close(); }
回答3:
One example of rawQuery - db.rawQuery("select * from table where column = ?",new String[]{"data"});
回答4:
see below code it may help you.
String q = "SELECT * FROM customer"; Cursor mCursor = mDb.rawQuery(q, null);
or
String q = "SELECT * FROM customer WHERE _id = " + customerDbId ; Cursor mCursor = mDb.rawQuery(q, null);
回答5:
For completeness and correct resource management:
ICursor cursor = null; try { cursor = db.RawQuery("SELECT * FROM " + RECORDS_TABLE + " WHERE " + RECORD_ID + "=?", new String[] { id + "" }); if (cursor.Count > 0) { cursor.MoveToFirst(); } return GetRecordFromCursor(cursor); // Copy cursor props to custom obj } finally // IMPORTANT !!! Ensure cursor is not left hanging around ... { if(cursor != null) cursor.Close(); }
回答6:
String mQuery = "SELECT Name,Family From tblName"; Cursor mCur = db.rawQuery(mQuery, new String[]{}); mCur.moveToFirst(); while ( !mCur.isAfterLast()) { String name= mCur.getString(mCur.getColumnIndex("Name")); String family= mCur.getString(mCur.getColumnIndex("Family")); mCur.moveToNext(); }
Name and family are your result
回答7:
if your SQL query is this
SELECT name,roll FROM student WHERE name='Amit' AND roll='7'
then rawQuery will be
String query="SELECT id, name FROM people WHERE name = ? AND roll = ?"; String[] selectionArgs = {"Amit","7"} db.rawQuery(query, selectionArgs);