Android教程之SQlit数据库操作 (3)
db.execSQL(sql_3);
setTitle("insert records ok!");
} catch (SQLException e) {
Log.e("ERROR", e.toString());
}
采用第二种方便的方法进行插入:(采用contentValues的方法)
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("stud_no", "S108");
cv.put("stud_name", "Lily Chen");
db.insert("Student", null, cv);
cv = new ContentValues();
cv.put("stud_no", "S201");
cv.put("stud_name", "Tom Kao");
db.insert("Student", null, cv);
cv = new ContentValues();
cv.put("stud_no", "S333");
cv.put("stud_name", "Peter Rabbit");
db.insert("Student", null, cv);
setTitle("insert record ok!");
查询语句:
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
String col[] = { "stud_no", "stud_name" };
cur = db.query("Student", col, null, null, null, null, null);
Integer n = cur.getCount();
String ss = Integer.toString(n);
setTitle(ss + " records");
cur.moveToFirst();
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
selection: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
selectionArgs:You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
如何不断循环的取出下一条打印出来:
查询语句返回的是一个Cursor cur = db.query("MyOrder", col, cond, null, null, null,null);第一个参数为数据表的名字,第二个参数为一个String[],里边是数据列的名字,第三个是查询条件。翻译的SQL语句为://SELECT order_no, type FROM MyOrder WHERE type='NEW'。
相关新闻>>
- 发表评论
-
- 最新评论 更多>>