rawQuery(query, selectionArgs)

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!