創建一個數據庫
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 List<Person> findAll()
{
SQLiteDatabase db = sqlite.getReadableDatabase();
List<Person> persons = new ArrayList<Person>();
//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());
List<Person> persons = dao.findAll();
for(Person p : persons)
{
System.out.println(p.toString());
}
}
}
復制代碼
測試權限
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.yuyidong.sqlite">
</instrumentation>
<uses-library android:name="android.test.runner"/>