SQLite特点:
1.零配置
2.可移植
3.紧凑
4.简单
5.可靠
SQLite可以解析的sql语句:
查询/排序/分页/插入/更新/删除
使用SQLiteOpenHelper抽象类操作数据库:
SQLiteOpenHelper提供了两个重要方法:
onCreate(SQLiteDatabase db); onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion);
//会以读写方式获得SQLiteDatabase 实例,若数据库未创建则调用onCreate(SQLiteDatabase db)方法创建数据库,
//若版本号变化则会触发onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
getWritableDatabase();
//会以只读方式获得SQLiteDatabase 实例,若版本号变化则会触发onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion);
getReadableDatabase();
//版本号只能累加
增删该查有两种实现方式
DatabaseHelper.java 代码如下:
package com.example.service_basic.service; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DatabaseHelper extends SQLiteOpenHelper{ private static final String TAG = DatabaseHelper.class.getSimpleName(); private static final String NAME = "test.db"; private static final int VERSION = 2; public DatabaseHelper(Context context) { super(context, NAME, null, VERSION); Log.i(TAG, "DatabaseHelper()..执行了构造函数"); } @Override public void onCreate(SQLiteDatabase db) { Log.i(TAG, "onCreate().."); db.execSQL("create table person (personid integer primary key autoincrement,name varchar(20),age integer)"); } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { Log.i(TAG, "onUpgrade().."); } }
Person.java 代码如下
package com.example.service_basic.domain; public class Person { private Integer id; private String name; private Integer age; public Person() { super(); } public Person(String name, Integer age) { super(); this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
第一种方式实现:
package com.example.service_basic.service; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.service_basic.domain.Person; public class PersonService { private DatabaseHelper databaseHelper; private Context context; public PersonService(Context context){ this.context = context; databaseHelper = new DatabaseHelper(this.context); } public void save(Person person){ SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.beginTransaction(); db.execSQL("insert into person (name,age) values(?,?) ",new Object[]{person.getName(),person.getAge()}); db.execSQL("insert into person (name,age) values(?,?) ",new Object[]{person.getName(),person.getAge()}); db.setTransactionSuccessful(); db.endTransaction(); } public void update(Person person){ SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.execSQL("update person set name = ?,age = ? where personid = ? ",new Object[]{person.getName(),person.getAge(),person.getId()}); } public void delete(Integer id){ SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.execSQL("delete from person where personid=? ",new Object[]{id}); } public Person query(String name){ SQLiteDatabase db = databaseHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from person where name = ?", new String[]{name}); if(cursor.moveToNext()){ Person person = new Person(); person.setId(cursor.getInt(cursor.getColumnIndex("personid"))); person.setName(cursor.getString(cursor.getColumnIndex("name"))); person.setAge(cursor.getInt(cursor.getColumnIndex("age"))); cursor.close(); return person; } cursor.close(); return null; } public List<Person> getPageData(int firstResult,int maxResult){ List<Person> persons = new ArrayList<Person>(); SQLiteDatabase db = databaseHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select personid,name,age from person limit ?,? ", new String[]{ String.valueOf(firstResult), String.valueOf(maxResult) }); while(cursor.moveToNext()){ Person person = new Person(); person.setId(cursor.getInt(cursor.getColumnIndex("personid"))); person.setName(cursor.getString(cursor.getColumnIndex("name"))); person.setAge(cursor.getInt(cursor.getColumnIndex("age"))); persons.add(person); } cursor.close(); return persons; } public int getCountByPerson(){ SQLiteDatabase db = databaseHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select count(1) from person ", null); cursor.moveToFirst(); int count = cursor.getInt(0); cursor.close(); return count; } }
第一种方式单元测试类:
package com.example.service_basic.test; import java.util.List; import android.test.AndroidTestCase; import android.util.Log; import com.example.service_basic.domain.Person; import com.example.service_basic.service.PersonService; public class TestPersonService extends AndroidTestCase{ private static final String TAG = TestPersonService.class.getSimpleName(); public void testSave(){ PersonService service = new PersonService(getContext()); for(int i=0;i<10;i++){ Person person = new Person("Hello"+i,i); Log.i(TAG, "testSave()"+i+"..begin.."); service.save(person); Log.i(TAG, "testSave()."+i+".end.."); } } public void testUpdate(){ PersonService service = new PersonService(getContext()); Person person = new Person("Charles",32); person.setId(1); Log.i(TAG, "testUpdate()..begin.."); service.update(person); Log.i(TAG, "testUpdate()..end.."); } public void testDelete(){ PersonService service = new PersonService(getContext()); Log.i(TAG, "testDelete()..begin.."); service.delete(1); Log.i(TAG, "testDelete()..end.."); } public void testRawQuery(){ PersonService service = new PersonService(getContext()); Person person = service.query("carl"); Log.i(TAG, "Person..RawQuery()..:"+person); } public void testGetPageData(){ PersonService service = new PersonService(getContext()); List<Person> persons = service.getPageData(0, 100); for(Person p:persons){ Log.i(TAG, "person:"+p); } } public void testGetCountByPerson(){ PersonService service = new PersonService(getContext()); int count = service.getCountByPerson(); Log.i(TAG, "person_count:"+count); } }
第二种实现方式:
package com.example.service_basic.service; 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; import com.example.service_basic.domain.Person; public class OtherPersonService { private DatabaseHelper databaseHelper; private Context context; public OtherPersonService(Context context){ this.context = context; databaseHelper = new DatabaseHelper(this.context); } public void save(Person person){ SQLiteDatabase db = databaseHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", person.getName()); values.put("age", person.getAge()); db.insert("person", "name,age", values); db.close(); } public void update(Person person){ SQLiteDatabase db = databaseHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", person.getName()); values.put("age", person.getAge()); db.update("person", values, "personid = ? ", new String[]{String.valueOf(person.getId())}); db.close(); } public void delete(Integer id){ SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.delete("person", "personid=?", new String[]{String.valueOf(id)}); db.close(); } public Person find(Integer id){ SQLiteDatabase db = databaseHelper.getReadableDatabase(); Cursor cursor = db.query("person", new String[]{"personid","name","age"}, "personid = ? ", new String[]{String.valueOf(id)}, null, null, null); if(cursor.moveToNext()){ Person person = new Person(); person.setId(cursor.getInt(0)); person.setName(cursor.getString(1)); person.setAge(cursor.getInt(2)); db.close(); return person; } db.close(); return null; } public int getCount(){ int count = 0; SQLiteDatabase db = databaseHelper.getReadableDatabase(); Cursor cursor = db.query("person", new String[]{"count(*)"}, null, null, null, null, null); cursor.moveToFirst(); count = cursor.getInt(0); db.close(); return count; } public List<Person> getPageData(int startNo,int endNo){ List<Person> list = new ArrayList<Person>(); SQLiteDatabase db = databaseHelper.getReadableDatabase(); Cursor cursor = db.query("person", new String[]{"personid","name","age"}, null, null, null, null, null, startNo+","+endNo); while(cursor.moveToNext()){ Person person = new Person(); person.setId(cursor.getInt(0)); person.setName(cursor.getString(1)); person.setAge(cursor.getInt(2)); list.add(person); } db.close(); return list; } }
第二种实现方式单元测试类:
package com.example.service_basic.test; import java.util.List; import android.test.AndroidTestCase; import android.util.Log; import com.example.service_basic.domain.Person; import com.example.service_basic.service.OtherPersonService; public class TestOtherPersonService extends AndroidTestCase{ private static final String TAG = TestOtherPersonService.class.getSimpleName(); public void testOtherSave(){ OtherPersonService service = new OtherPersonService(getContext()); Log.i(TAG, "testOtherSave()..begin.."); Person person = new Person("Jerry_",18); service.save(person); Log.i(TAG, "testOtherSave()..end.."); } public void testOtherUpdate(){ OtherPersonService service = new OtherPersonService(getContext()); Log.i(TAG, "testOtherUpdate()..begin.."); Person person = new Person("Jerry_",18); person.setId(2); service.update(person); Log.i(TAG, "testOtherUpdate()..end.."); } public void testOtherDelete(){ OtherPersonService service = new OtherPersonService(getContext()); Log.i(TAG, "testOtherDelete()..begin.."); service.delete(2); Log.i(TAG, "testOtherDelete()..end.."); } public void testOtherfind(){ OtherPersonService service = new OtherPersonService(getContext()); Log.i(TAG, "testOtherfind()..begin.."); Person person = service.find(2); Log.i(TAG, "person:"+person); Log.i(TAG, "testOtherfind()..end.."); } public void testOtherCount(){ OtherPersonService service = new OtherPersonService(getContext()); Log.i(TAG, "testOtherCount()..begin.."); int count = service.getCount(); Log.i(TAG, "count:"+count); Log.i(TAG, "testOtherCount()..end.."); } public void testOtherGetPageData(){ OtherPersonService service = new OtherPersonService(getContext()); Log.i(TAG, "testOtherCount()..begin.."); List<Person> persons = service.getPageData(5, 5); for(Person p:persons){ Log.i(TAG, "person:"+p); } Log.i(TAG, "testOtherCount()..end.."); } }