Android教程之SQlit数据库操作 (4)
得到查询出来数据的个数。Integer n = cur.getCount();
如果想从头遍历的话:cur.moveToFirst();
如果想遍历完的话可以这么做:
while (!cur.isAfterLast()) { String ss = cur.getString(0) + ", " + cur.getString(1);
data[k++] = ss;
cur.moveToNext();
}
public String getString(int columnIndex)
对于当前条处理完了,想进入下一条目。moveToNext();
code example
public String[] loadData() {
SharedPreferences passwdfile = m_ctx.getSharedPreferences("CONDITION",
0);
String cond = passwdfile.getString("CONDITION", null);
mOpenHelper = new DatabaseHelper(m_ctx);
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
String col[] = { "order_no", "type" };
Cursor cur = db.query("MyOrder", col, cond, null, null, null, null);
//SELECT order_no, type FROM MyOrder WHERE type='NEW'
Integer n = cur.getCount();
String[] data = new String[n];
cur.moveToFirst();
int k = 0;
while (!cur.isAfterLast()) {
String ss = cur.getString(0) + ", " + cur.getString(1);
data[k++] = ss;
cur.moveToNext();
}
return data;
}
更新:
//更新条列
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("stud_no", "S288");
cv.put("stud_name", "Linda Wang");
db.update("Student", cv, "stud_no = 'S201'", null);
删除:
//删除条列
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
db.delete("Student", "stud_no = 'S108'", null);
相关新闻>>
- 发表评论
-
- 最新评论 进入详细评论页>>