创建一个数据库
public PersonSQLite(Context context) { super(context, "person.db", null, 1); // TODO 自动生成的构造函数存根 }
背景
这里以一个Person类来举例,Person类里面有成员变量,数据库的列也是这几项。
Person类
private int id; private String name; private String number;
SQL语句类
private PersonSQLite sqlite; public PersonDao2(Context context) { sqlite = new PersonSQLite(context); } public long add(String name ,String number) { SQLiteDatabase db = sqlite.getWritableDatabase(); //db.execSQL("insert into person (name,number) values (?,?)", new Object[]{name,number}); ContentValues values = new ContentValues(); values.put("name", name); values.put("number", number); long id = db.insert("person", null, values); db.close(); return id; } public boolean find(String name) { SQLiteDatabase db = sqlite.getReadableDatabase(); //Cursor cursor = db.rawQuery("select * from person where name=?", new String[]{name}); Cursor cursor = db.query("person", null,"name=?", new String[]{name}, null, null, null); boolean result = cursor.moveToNext(); cursor.close(); db.close(); return result; } public int update(String name , String newnumber) { SQLiteDatabase db = sqlite.getWritableDatabase(); //db.execSQL("update person set number=? where name =?",new Object[]{newnumber,name}); ContentValues values = new ContentValues(); values.put("number", newnumber); int number = db.update("person", values, "name=?", new String[]{newnumber}); db.close(); return number; } public int delete(String name) { SQLiteDatabase db = sqlite.getWritableDatabase(); //db.execSQL("delete from person where name = ?",new Object[]{name}); int number = db.delete("person", "name=?", new String[]{name}); db.close(); return number; } public ListfindAll() { SQLiteDatabase db = sqlite.getReadableDatabase(); List persons = new ArrayList (); //Cursor cursor = db.rawQuery("select name,id,number from person",null); Cursor cursor = db.query("person", new String[]{"name","id","number"}, null, null, null, null, null); while(cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String number = cursor.getString(cursor.getColumnIndex("number")); Person p = new Person(id,name,number); persons.add(p); } cursor.close(); db.close(); return persons; }
这些方法的作用通过名字可以理解的吧~~然后注释掉的语句是直接通过SQL语句写的,另外一种写法就是调用官方的网API。里面比如db.query,还有db.delete等。
测试类
public class testDB extends AndroidTestCase { public void testCreateDB() throws Exception { PersonSQLite sqlite = new PersonSQLite(getContext()); sqlite.getWritableDatabase(); } public void testAdd() throws Exception { PersonDao dao = new PersonDao(getContext()); dao.add("wangwu","123321"); } public void testFind() { PersonDao dao = new PersonDao(getContext()); boolean result = dao.find("wangwu"); assertEquals(true, result); } public void testUpdate() { PersonDao dao = new PersonDao(getContext()); dao.update("wangwu", "122222222"); } public void testeDelete() { PersonDao dao = new PersonDao(getContext()); dao.delete("wangwu"); } public void testFindAll() { PersonDao dao = new PersonDao(getContext()); Listpersons = dao.findAll(); for(Person p : persons) { System.out.println(p.toString()); } } }
测试权限
我是天王盖地虎的分割线
之前写过一个Sqlite的博客,但是没有这样测试着弄,是直接操作的,所以可能会有问题,这里再发一帖更正以前的错误。
源代码:
SQLite1.zip
转载请注明出处: