作者:蜜小嘟_546 | 来源:互联网 | 2023-05-18 17:31
SQLite数据库存储数据的时候,首先必须继承SQLiteOpenHelper类:[java]viewplaincopyprint?publicclassDbOpenHelperextend
SQLite数据库存储数据的时候,首先必须继承SQLiteOpenHelper类:
[java] view plaincopyprint?
- public class DbOpenHelper extends SQLiteOpenHelper {
-
- public DbOpenHelper(Context context) {
-
-
-
-
-
-
-
-
-
- super(context, "andy.db", null, 2);
- }
-
-
-
-
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("create table person(id integer primary key autoincrement, name varchar(20), age integer)");
- }
-
-
-
-
-
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("alter table person add phone varchar(12) null");
- }
-
- }
之后,对数据库进行操作:
[java] view plaincopyprint?
- public class PersonService {
- private DbOpenHelper helper;
-
- public PersonService(Context context) {
- helper = new DbOpenHelper(context);
- }
-
-
- public void addData(Person person) {
- SQLiteDatabase db = helper.getWritableDatabase();
- db.execSQL("insert into person (name, age, phone) values (?, ?, ?)",
- new Object[] { person.getName(), person.getAge(), person.getPhone() });
- }
-
-
- public void deleteData(Integer id) {
- SQLiteDatabase db = helper.getWritableDatabase();
- db.execSQL("delete from person where id = ?", new Object[] { id });
- }
-
-
- public void updateData(Person person) {
- SQLiteDatabase db = helper.getWritableDatabase();
- db.execSQL("update person set name=?,age=?,phOne=? where id = ?",
- new Object[] { person.getName(), person.getAge(), person.getPhone(), person.getId() });
- }
-
-
- public Person checkData(Integer id) {
- SQLiteDatabase db = helper.getReadableDatabase();
- Cursor cursor = db.rawQuery("select * from person where id = ?",
- new String[] { id.toString() });
- if (cursor.moveToFirst()) {
- int personid = cursor.getInt(cursor.getColumnIndex("id"));
- String name = cursor.getString(cursor.getColumnIndex("name"));
- String phone = cursor.getString(cursor.getColumnIndex("phone"));
- short age = cursor.getShort(cursor.getColumnIndex("age"));
- if(cursor != null)
- cursor.close();
- return new Person(personid, name, phone, age);
- }
- return null;
- }
-
-
- public int getCount() {
- SQLiteDatabase db = helper.getReadableDatabase();
- Cursor cursor = db.rawQuery("select count(*) from person", null);
- cursor.moveToFirst();
- int count = cursor.getInt(0);
-
- if(cursor != null)
- cursor.close();
-
- return count;
- }
-
-
-
-
-
-
- public List getScrollData(int offset, int maxCount) {
- List persons = new ArrayList();
-
- SQLiteDatabase db = helper.getReadableDatabase();
- Cursor cursor = db.rawQuery("select * from person limit ?,?",
- new String[]{String.valueOf(offset), String.valueOf(maxCount)});
-
- while(cursor.moveToNext()){
- int personid = cursor.getInt(cursor.getColumnIndex("id"));
- String name = cursor.getString(cursor.getColumnIndex("name"));
- String phone = cursor.getString(cursor.getColumnIndex("phone"));
- short age = cursor.getShort(cursor.getColumnIndex("age"));
- persons.add(new Person(personid, name, phone, age));
- }
-
- if(cursor != null){
- cursor.close();
- }
- return persons;
- }
- }
之后,通过单元测试来测试所编写的数据库代码:
[java] view plaincopyprint?
- public class DbOpenHelperTest extends AndroidTestCase {
- private final static String TAG = "DbOpenHelperTest";
-
- public void testCreateDatabase() throws Throwable{
- DbOpenHelper helper = new DbOpenHelper(this.getContext());
- helper.getWritableDatabase();
- }
-
- public void testAddData() throws Throwable{
- PersonService service = new PersonService(this.getContext());
-
- Person person = new Person();
- person.setName("hanmeimei");
- person.setAge((short)12);
- person.setPhone("12738487675");
- service.addData(person);
-
- person.setName("lilei");
- person.setAge((short)15);
- person.setPhone("1273834275");
- service.addData(person);
-
- person.setName("mingming");
- person.setAge((short)34);
- person.setPhone("12737857675");
- service.addData(person);
-
- person.setName("lile");
- person.setAge((short)32);
- person.setPhone("12783947675");
- service.addData(person);
-
- person.setName("mashan");
- person.setAge((short)22);
- person.setPhone("12709847675");
- service.addData(person);
- }
-
- public void testDeleteData() throws Throwable{
- PersonService service = new PersonService(this.getContext());
- service.deleteData(1);
- }
-
- public void testUpdateData() throws Throwable{
- PersonService service = new PersonService(this.getContext());
- Person person = service.checkData(3);
- person.setName("linsan");
- service.updateData(person);
- }
-
- public void testCheckData() throws Throwable{
- PersonService service = new PersonService(this.getContext());
- Person person = service.checkData(3);
-
- Log.d(TAG, person.toString());
- }
-
- public void testGetCount() throws Throwable{
- PersonService service = new PersonService(this.getContext());
- int count = service.getCount();
- Log.d(TAG, "count = " + count);
- }
-
- public void testGetScrollData() throws Throwable{
- PersonService service = new PersonService(this.getContext());
- List persons = service.getScrollData(0, 5);
- for(Person person : persons){
- Log.d(TAG, person.toString());
- }
- }
- }