rawQuery(查询,selectionArgs)

我想使用select查询从表中检索数据。 我发现, SQLiteDatabase类的rawQuery(query, selectionArgs)方法检索数据。 但我不知道如何将queryselectionArgs传递给rawQuery方法?

 rawQuery("SELECT id, name FROM people WHERE name = ? AND id = ?", new String[] {"David", "2"}); 

你传递一个具有相同数量的元素的string数组,如“?”

也许这可以帮助你

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

一个rawQuery的例子 – db.rawQuery("select * from table where column = ?",new String[]{"data"});

看下面的代码它可以帮助你。

 String q = "SELECT * FROM customer"; Cursor mCursor = mDb.rawQuery(q, null); 

要么

 String q = "SELECT * FROM customer WHERE _id = " + customerDbId ; Cursor mCursor = mDb.rawQuery(q, null); 
 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(); } 

姓名和家庭是你的结果

为了完整和正确的资源pipe理:

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