sqlite3 数据库实战并测试
来源:互联网 发布:淘宝文胸推荐知乎 编辑:程序博客网 时间:2024/06/05 19:43
实体类:com.itheima28.sqlitedemo.entities包public class Person { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person() { super(); // TODO Auto-generated constructor stub } public Person(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; }}
数据库帮助类com.itheima28.sqlitedemo.db包:/** * @author andong * 数据库帮助类, 用于创建和管理数据库的. */public class PersonSQLiteOpenHelper extends SQLiteOpenHelper { private static final String TAG = "PersonSQLiteOpenHelper"; /** * 数据库的构造函数 :用来定义数据库的名称 ,数据库查询的结果,数据库的版本 * @param context :作用:数据库在哪里进行创建 * * name 数据库名称 * factory 游标工程 * version 数据库的版本号 不可以小于1 */ public PersonSQLiteOpenHelper(Context context) { super(context, "itheima28.db", null, 2); } /** * 数据库第一次创建时回调此方法. * 初始化一些表 */ @Override public void onCreate(SQLiteDatabase db) { // 操作数据库 String sql = "create table person(_id integer primary key, name varchar(20), age integer);"; db.execSQL(sql); // 创建person表 } /** * 数据库的版本号更新时回调此方法, * 更新数据库的内容(删除表, 添加表, 修改表) */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if(oldVersion == 1 && newVersion == 2) { Log.i(TAG, "数据库更新啦"); // 在person表中添加一个余额列balance db.execSQL("alter table person add balance integer;"); } }}
DAO类com.itheima28.sqlitedemo.dao:public class PersonDao { private PersonSQLiteOpenHelper mOpenHelper; // 数据库的帮助类对象 public PersonDao(Context context) { mOpenHelper = new PersonSQLiteOpenHelper(context); } /** * 添加到person表一条数据 * @param person */ public void insert(Person person) { SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()) { // 如果数据库打开, 执行添加的操作 // 执行添加到数据库的操作 db.execSQL("insert into person(name, age) values(?, ?);", new Object[]{person.getName(), person.getAge()}); db.close(); // 数据库关闭 } } /** * 更据id删除记录 * @param id */ public void delete(int id) { SQLiteDatabase db = mOpenHelper.getWritableDatabase(); // 获得可写的数据库对象 if(db.isOpen()) { // 如果数据库打开, 执行添加的操作 db.execSQL("delete from person where _id = ?;", new Integer[]{id}); db.close(); // 数据库关闭 } } /** * 根据id找到记录, 并且修改姓名 * @param id * @param name */ public void update(int id, String name) { SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()) { // 如果数据库打开, 执行添加的操作 db.execSQL("update person set name = ? where _id = ?;", new Object[]{name, id}); db.close(); // 数据库关闭 } } public List<Person> queryAll() { SQLiteDatabase db = mOpenHelper.getReadableDatabase(); // 获得一个只读的数据库对象 if(db.isOpen()) { Cursor cursor = db.rawQuery("select _id, name, age from person;", null); if(cursor != null && cursor.getCount() > 0) { List<Person> personList = new ArrayList<Person>(); int id; String name; int age; while(cursor.moveToNext()) { id = cursor.getInt(cursor.getColumnIndex("_id")); // 取第0列的数据 id name = cursor.getString(cursor.getColumnIndex("name")); // 取姓名 age = cursor.getInt(cursor.getColumnIndex("age")); // 取年龄 personList.add(new Person(id, name, age)); } cursor.close(); db.close(); return personList; } db.close(); } return null; } /** * 根据id查询人 * @param id * @return */ public Person queryItem(int id) { SQLiteDatabase db = mOpenHelper.getReadableDatabase(); // 获得一个只读的数据库对象 if(db.isOpen()) { Cursor cursor = db.rawQuery("select _id, name, age from person where _id = ?;", new String[]{String.valueOf(id)}); if(cursor != null && cursor.moveToFirst()) { int _id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); cursor.close(); db.close(); return new Person(_id, name, age); } db.close(); } return null; }}public class PersonDao2 { private static final String TAG = "PersonDao2"; private PersonSQLiteOpenHelper mOpenHelper; // 数据库的帮助类对象 public PersonDao2(Context context) { mOpenHelper = new PersonSQLiteOpenHelper(context); } /** * 添加到person表一条数据 * @param person */ public void insert(Person person) { SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()) { // 如果数据库打开, 执行添加的操作 ContentValues values = new ContentValues(); values.put("name", person.getName()); // key作为要存储的列名, value对象列的值 values.put("age", person.getAge()); long id = db.insert("person", "name", values); Log.i(TAG, "id: " + id); db.close(); // 数据库关闭 } } /** * 更据id删除记录 * @param id */ public void delete(int id) { SQLiteDatabase db = mOpenHelper.getWritableDatabase(); // 获得可写的数据库对象 if(db.isOpen()) { // 如果数据库打开, 执行添加的操作 String whereClause = "_id = ?"; String[] whereArgs = {id + ""}; int count = db.delete("person", whereClause, whereArgs); Log.i(TAG, "删除了: " + count + "行"); db.close(); // 数据库关闭 } } /** * 根据id找到记录, 并且修改姓名 * @param id * @param name */ public void update(int id, String name) { SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()) { // 如果数据库打开, 执行添加的操作 ContentValues values = new ContentValues(); values.put("name", name); int count = db.update("person", values, "_id = ?", new String[]{id + ""}); Log.i(TAG, "修改了: " + count + "行"); db.close(); // 数据库关闭 } } public List<Person> queryAll() { SQLiteDatabase db = mOpenHelper.getReadableDatabase(); // 获得一个只读的数据库对象 if(db.isOpen()) { String[] columns = {"_id", "name", "age"}; // 需要的列 String selection = null; // 选择条件, 给null查询所有 String[] selectionArgs = null; // 选择条件的参数, 会把选择条件中的? 替换成数据中的值 String groupBy = null; // 分组语句 group by name String having = null; // 过滤语句 String orderBy = null; // 排序 Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy); int id; String name; int age; if(cursor != null && cursor.getCount() > 0) { List<Person> personList = new ArrayList<Person>(); while(cursor.moveToNext()) { // 向下移一位, 知道最后一位, 不可以往下移动了, 停止. id = cursor.getInt(0); name = cursor.getString(1); age = cursor.getInt(2); personList.add(new Person(id, name, age)); } db.close(); return personList; } db.close(); } return null; } /** * 根据id查询人 * @param id * @return */ public Person queryItem(int id) { SQLiteDatabase db = mOpenHelper.getReadableDatabase(); // 获得一个只读的数据库对象 if(db.isOpen()) { String[] columns = {"_id", "name", "age"}; // 需要的列 String selection = "_id = ?"; // 选择条件, 给null查询所有 String[] selectionArgs = {id + ""}; // 选择条件的参数, 会把选择条件中的? 替换成数据中的值 String groupBy = null; // 分组语句 group by name String having = null; // 过滤语句 String orderBy = null; // 排序 Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy); if(cursor != null && cursor.moveToFirst()) { // cursor对象不为null, 并且可以移动到第一行 int _id = cursor.getInt(0); String name = cursor.getString(1); int age = cursor.getInt(2); db.close(); return new Person(_id, name, age); } db.close(); } return null; }}
测试类com.itheima28.sqlitedemo.testpublic class TestCase extends AndroidTestCase { private static final String TAG = "TestCase"; public void test() { // 数据库什么时候创建 PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext()); // 第一次连接数据库时创建数据库文件. onCreate会被调用 openHelper.getReadableDatabase(); } public void testInsert() { PersonDao dao = new PersonDao(getContext()); dao.insert(new Person(1, "冠希", 28)); } public void testDelete() { PersonDao dao = new PersonDao(getContext()); dao.delete(1); } public void testUpdate() { PersonDao dao = new PersonDao(getContext()); dao.update(3, "凤姐"); } public void testQueryAll() { PersonDao dao = new PersonDao(getContext()); List<Person> personList = dao.queryAll(); for (Person person : personList) { Log.i(TAG, person.toString()); } } public void testQueryItem() { PersonDao dao = new PersonDao(getContext()); Person person = dao.queryItem(4); Log.i(TAG, person.toString()); } public void testTransaction() { PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext()); SQLiteDatabase db = openHelper.getWritableDatabase(); if(db.isOpen()) { try { // 开启事务 db.beginTransaction(); // 1. 从张三账户中扣1000块钱 db.execSQL("update person set balance = balance - 1000 where name = 'zhangsan';"); // ATM机, 挂掉了. // int result = 10 / 0; // 2. 向李四账户中加1000块钱 db.execSQL("update person set balance = balance + 1000 where name = 'lisi';"); // 标记事务成功 db.setTransactionSuccessful(); } finally { // 停止事务 db.endTransaction(); } db.close(); } } public void testTransactionInsert() { PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext()); SQLiteDatabase db = openHelper.getWritableDatabase(); if(db.isOpen()) { // 1. 记住当前的时间 long start = System.currentTimeMillis(); // 2. 开始添加数据 try { db.beginTransaction(); for (int i = 0; i < 10000; i++) { db.execSQL("insert into person(name, age, balance) values('wang" + i + "', " + (10 + i) + ", " + (10000 + i) + ")"); } db.setTransactionSuccessful(); } finally { db.endTransaction(); } // 3. 记住结束时间, 计算耗时时间 long end = System.currentTimeMillis(); long diff = end - start; Log.i(TAG, "耗时: " + diff + "毫秒"); db.close(); } }}
0 0
- sqlite3 数据库实战并测试
- Ubuntu12.04交叉编译mini2440提供的QtE4.6.3并测试sqlite3数据库
- 数据库——SQLite3编译及测试
- sqlite3测试打开关闭数据库的耗时
- python读写sqlite3数据库并将统计数据写入excel
- sqlite3数据库
- SQLite3数据库
- sqlite3 数据库
- SQLite3数据库
- 数据库sqlite3
- Sqlite3数据库
- sqlite3数据库
- Sqlite3数据库
- Sqlite3数据库
- Sqlite3 和Sqlitedbms数据库环境搭建与性能测试
- sqlite3触发器的实战
- python sqlite3插入测试
- SQLite3 测试程序
- 各大公司笔试面试题集锦
- 详解UITableViewCell的重用机制和错误解决办法
- EXCEL工作表保护密码忘记,撤销保护攻略
- Python - I/O 文件读写 内存读写
- 《深入理解Linux内核》软中断/tasklet/工作队列
- sqlite3 数据库实战并测试
- [UVA 11762] Race to 1
- C语言数据结构之图的遍历
- NSString的小结
- 字符串算法
- linux patch 命令小结
- http协议学习笔记
- 线性表的实现(二)链式存储
- ccpc Sudoku(搜索)