import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class PersonDao { private DBOpenHelper helper; public PersonDao(Context context) { helper = new DBOpenHelper(context); } public void insert(Person p) { SQLiteDatabase db = helper.getWritableDatabase(); // 获取数据库连接(可写的) ContentValues values = new ContentValues(); // 类似于Map的容器, 键是String, 用来存放列名, 值是Object, 用来存要插入的数据 values.put("name", p.getName()); // 某些情况下, 程序会接收一个ContentValues参数, 这时用这种方式存储比较方便 values.put("balance", p.getBalance()); long id = db.insert("person", null, values); // 第二个参数随便写表中的一个列名即可, 用来在想插入一条除了主键全部为空的记录时使用 System.out.println("插入的记录的id是: " + id); db.close(); } public void delete(int id) { SQLiteDatabase db = helper.getWritableDatabase(); int rows = db.delete("person", "id=?", new String[] { id + "" }); System.out.println("删除了" + rows + "行"); db.close(); } public void update(Person p) { SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", p.getName()); values.put("balance", p.getBalance()); int rows = db.update("person", values, "id=?", new String[] { p.getId() + "" }); System.out.println("更新了" + rows + "行"); db.close(); } public Person query(int id) { SQLiteDatabase db = helper.getReadableDatabase(); // 获取数据库连接(可读的) Cursor c = db.query("person", new String[] { "name", "balance" }, "id=?", new String[] { id + "" }, null, null, null); Person p = null; if (c.moveToNext()) { // 判断游标是否包含下一条记录, 如果包含将游标向后移动一位 String name = c.getString(c.getColumnIndex("name")); // 获取"name"字段的索引, 然后根据索引获取数据 int balance = c.getInt(1); // 获取1号索引上的数据 p = new Person(id, name, balance); } c.close(); db.close(); return p; } public List<Person> queryAll() { SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.query("person", null, null, null, null, null, "id DESC"); List<Person> persons = new ArrayList<Person>(); while (c.moveToNext()) { Person p = new Person(c.getInt(0), c.getString(1), c.getInt(2)); persons.add(p); } c.close(); db.close(); return persons; } public int queryCount() { SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.query("person", new String[]{ "COUNT(*)" }, null, null, null, null, null); c.moveToNext(); int count = c.getInt(0); c.close(); db.close(); return count; } public List<Person> queryPage(int pageNum, int capacity) { String offset = (pageNum - 1) * capacity + ""; // 偏移量 String len = capacity + ""; // 个数 SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.query("person", null, null, null, null, null, null, offset + "," + len); List<Person> persons = new ArrayList<Person>(); while (c.moveToNext()) { Person p = new Person(c.getInt(0), c.getString(1), c.getInt(2)); persons.add(p); } c.close(); db.close(); return persons; } public void remit(int from, int to, int amount) { SQLiteDatabase db = helper.getWritableDatabase(); try { db.beginTransaction(); // 开始事务 db.execSQL("UPDATE person SET balance=balance-? WHERE id=?", new Object[] { amount, from }); db.execSQL("UPDATE person SET balance=balance+? WHERE id=?", new Object[] { amount, to }); db.setTransactionSuccessful(); // 设置成功点, 在事务结束时, 成功点之前的操作会被提交 } finally { db.endTransaction(); // 结束事务, 将成功点之前的操作提交 db.close(); } } }