//1数据库的增删查改,每次的操作都要打开数据库和关闭数据库.
//2在测试类中测试.注意在查询整个表的时候,因为要保存到一个集合里面,所以有个实体类.
//这是基本都是数据库操作语言db.execsql,db.rawQuery这两个方法.
package cn.itcast.db.dao; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; import android.util.Log; import cn.itcast.db.MyDBOpenHelper; import cn.itcast.db.domain.Person; public class PersonDao { private static final String TAG = "PersonDao"; private MyDBOpenHelper dbOpenHelper; // 在personDao被new出来的时候 就完成初始化 public PersonDao(Context context) { dbOpenHelper = new MyDBOpenHelper(context); // dbOpenHelper.getReadableDatabase() // dbOpenHelper.getWritableDatabase() } // 增删改查 /** * 往数据库添加一条数据 */ public void add(String name,String phone) { boolean result = find(name); if (result) return; sqliteDatabase db = dbOpenHelper.getWritableDatabase(); if (db.isOpen()) { db.execsql("insert into person (name,phone) values (?,?)",new Object[] { name,phone }); // 关闭数据库 释放数据库的链接 db.close(); } } /** * 查找数据库的操作 */ public boolean find(String name) { boolean result = false; sqliteDatabase db = dbOpenHelper.getReadableDatabase(); if (db.isOpen()) { Cursor cursor = db.rawQuery("select * from person where name=?",new String[] { name }); if (cursor.moveToFirst()) { int index = cursor.getColumnIndex("phone"); // 得到phone在表中是第几列 String phone = cursor.getString(index); Log.i(TAG,"phone =" + phone); result = true; } // 记得关闭掉 cursor cursor.close(); result = false; // 释放数据库的链接 db.close(); } return result; } /** * 删除一条记录 * * @param name */ public void delete(String name) { sqliteDatabase db = dbOpenHelper.getWritableDatabase(); if (db.isOpen()) { db.execsql("delete from person where name =?",new Object[] { name }); db.close(); } } /** * 更新一条记录 * */ public void update(String name,String newname,String newphone) { sqliteDatabase db = dbOpenHelper.getWritableDatabase(); if (db.isOpen()) { db.execsql("update person set name=?,phone=? where name=?",new Object[] { newname,newphone,name }); db.close(); } } /** * 查找全部,保存到list中. */ public List<Person> getAllPersons() { List<Person> persons=null; sqliteDatabase db = dbOpenHelper.getReadableDatabase(); if (db.isOpen()) { persons = new ArrayList<Person>(); Cursor cursor = db.rawQuery("select * from person ",null); while (cursor.moveToNext()) { Person person = new Person(); int nameindex = cursor.getColumnIndex("name"); int phoneindex = cursor.getColumnIndex("phone"); String name = cursor.getString(nameindex); String phone = cursor.getString(phoneindex); person.setName(name); person.setNumber(phone); persons.add(person); } cursor.close(); db.close(); } return persons; } }
<pre class="java" name="code">package cn.itcast.db.dao.test; import java.util.List; import cn.itcast.db.dao.PersonDao; import cn.itcast.db.domain.Person; import android.test.AndroidTestCase; public class TestPersonDao extends AndroidTestCase { PersonDao dao; // 测试在执行测试代码时候的流程 //1 .new TestPersonDao 框架new出来 //2. 调用 setUp() //3. testAdd() 这个时候 上下文 才被创建出来 /* @Override protected void setUp() throws Exception { dao = new PersonDao(getContext()); super.setUp(); }*/ public void testAdd() throws Exception{ PersonDao dao = new PersonDao(getContext()); for(int i=0;i<100;i++){ dao.add("lisi"+i,"123456789"+i); } } public void testdelete() throws Exception{ PersonDao dao = new PersonDao(getContext()); dao.delete("lisi99"); } public void testupdate() throws Exception{ PersonDao dao = new PersonDao(getContext()); dao.update("lisi98","wangwu","119"); } public void testFindAll() throws Exception{ PersonDao dao = new PersonDao(getContext()); List<Person> persons = dao.getAllPersons(); assertEquals(100,persons.size()); } }
package cn.itcast.db.domain; public class Person { String name; String number; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } }
原文链接:https://www.f2er.com/sqlite/200427.html