我想使用select查询从表中检索数据。我找到了rawQuery(query, selectionArgs)
类的SQLiteDatabase
方法来检索数据。但我不知道如何将query
和selectionArgs
传递给rawQuery
方法?
答案 0 :(得分:202)
rawQuery("SELECT id, name FROM people WHERE name = ? AND id = ?", new String[] {"David", "2"});
您传递的字符串数组具有相同数量的元素,因为您有“?”
答案 1 :(得分:14)
也许这可以帮到你
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();
}
答案 2 :(得分:10)
rawQuery的一个示例 - db.rawQuery("select * from table where column = ?",new String[]{"data"});
答案 3 :(得分:2)
为了完整性和正确的资源管理:
<style name="StackedAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="buttonBarButtonStyle">@style/StackedButtonBarButtonStyle</item>
</style>
<style name="StackedButtonBarButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:layout_gravity">right</item>
<item name="android:textAllCaps">false</item>
</style>
答案 4 :(得分:1)
请参阅以下代码,它可能会对您有所帮助。
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);
答案 5 :(得分:1)
如果您的SQL查询是
SELECT name,roll FROM student WHERE name='Amit' AND roll='7'
然后rawQuery将
String query="SELECT id, name FROM people WHERE name = ? AND roll = ?";
String[] selectionArgs = {"Amit","7"}
db.rawQuery(query, selectionArgs);
答案 6 :(得分:0)
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();
}
姓名和家庭是你的结果