转载请注明出处: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();
本文介绍了两种在SQLite中执行查询的方法:使用rawQuery方法和query方法。通过示例详细展示了如何使用占位符安全地构建SQL语句,并遍历查询结果。

3624

被折叠的 条评论
为什么被折叠?



