SQLite数据库存储数据

SQLite数据库存储数据的时候,首先必须继承SQLiteOpenHelper类:

public class DbOpenHelper extends SQLiteOpenHelper {

		public DbOpenHelper(Context context) {
		/*
		 * 一般是当DbOpenHelper类调用getReadableDatabase()
		 * 或者getWritableDatabase()方法的时候,会创建数据库
		 * 此方法中的四个参数分别代表:
		 * context: 上下文对象
		 * name: 数据库的名称
		 * CursorFactory: 游标工厂,为null的时候,表示使用android系统默认的游标工厂
		 * version: 代表版本
		 */
		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");
	}

}


之后,对数据库进行操作:

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;
	}

	//查找数据库中的person表中的数据的数目
	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;
	}

	/*
	 * 分页,就是取出数据库中跳过offset条数据的maxCount条数据,
	 * offset代表跳过的数据的条数,如3,表示跳过3条,从第四条开始取数据
	 * maxCount代表要取出的数据的条数
	 */
	public List<Person> getScrollData(int offset, int maxCount) {
		List<Person> persons = new ArrayList<Person>();
		
		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;
	}
}


之后,通过单元测试来测试所编写的数据库代码:

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<Person> persons = service.getScrollData(0, 5);
		for(Person person : persons){
			Log.d(TAG, person.toString());
		}
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值