轉載請註明出處:http://blog.csdn.net/catchingsun/article/details/49181331
方式一:
Cursor result = db.rawQuery(sql, new String[]{});
其中第一個參數爲sql語句,第二個參數爲佔位符
如:
int col = 23;
SQLiteDatabase db = null;
OperatePositionDatabase o = new OperatePositionDatabase();
String sql = "select * from table_name where column = ?";
Cursor result = db.rawQuery(sql, new String[]{col.toString()});
佔位符:在sql中有時我們會查找表中值與變量值相符的元素,如果用拼接的形式會即不安全,又不方便,這時我們便可採用佔位符。佔位符中的數組元素代表sql語句中“?”的部分。
查詢結果遊標默認指向0位置。
for (int i = 0; i < result.getCount(); i++) {
result.moveToPosition(i);
search_result[i] = result.getInt(result.getColumnIndex("ColumnName"));//可通過列名進行獲取查詢結果中符合要查找的所有元素
}
result.close();//關閉遊標
db.close();//關閉數據庫
方式二:
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Cursor c = db.query(Table, null, null, null, null, null, null);
c.moveToFirst();
if (c.moveToFirst()) {// 移到表頭
// 遍歷數據表
for (int i = 0; i < c.getCount(); i++) {
c.moveToPosition(i);
search_result[i] = result.getInt(result.getColumnIndex("ColumnName"));
}c.close();