前面已经说到了几种文件的操作如sharedreference,sdcard.实际上android还提供了另外的存储方式那就是sqlite。只要学习过数据库掌握这个也是没问题的。下面就和我一起来弄一下这个吧。
1.安装一个SQLiteDeveloper,这个用来打开android生成的数据库。软件随便搜索就能找到,后面导出数据库只需打开软件点击“数据库“----->"注册数据库"后面的操作最好自己摸索吧
2.编写一个业务类先生成一个数据库和数据库表,这个类如下所示
package org.lxh.service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
public class DBService extends SQLiteOpenHelper { //使用SQLiteOpenHelper创建数据库
public DBService(Context context) {
super(context, "mldn.db", null, 1);
}
public void onCreate(SQLiteDatabase db) {
String sql="create table student(userid Integer primary key autoincrement,username varchar(30))"; //生成数据库表的sql
db.execSQL(sql); //生成表
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
数据库和表准备好之后就可以做下一步的工作了
3.编写增删改查的业务类
package org.lxh.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.lxh.vo.Student;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class StudentService {
private DBService db;
public StudentService(Context context){ //构造方法实例化DBService
db=new DBService(context);
}
public void savePerson(List<Student> stu){
String sql="insert into student(username) values(?)";
SQLiteDatabase database=db.getWritableDatabase(); //使用getWritableDatabase取得SQLiteDatabase
Iterator<Student> it=stu.iterator();
while(it.hasNext()){
Student student=it.next();
database.execSQL(sql, new Object[]{student.getUsername()}); //执行插入
}
}
public void delete(int userid){
String sql="delete from student where userid=?";
SQLiteDatabase database=db.getWritableDatabase();
database.execSQL(sql, new Object[]{String.valueOf(userid)});
}
public List<Student> fiandAll(){
List<Student> all=new ArrayList<Student>();
String sql="select * from student";
SQLiteDatabase database=db.getReadableDatabase(); //使用getReadableDatabase取得SQLiteDatabase
Cursor cursor=database.rawQuery(sql, null); //得到游标,类似resultset
Student stu;
while(cursor.moveToNext()){ //移动游标
int id=cursor.getInt(cursor.getColumnIndex("userid"));
String name=cursor.getString(cursor.getColumnIndex("username"));
stu=new Student();
stu.setUserid(id);
stu.setUsername(name);
all.add(stu);
}
cursor.close(); //关闭游标
return all;
}
public int getCount(){
String sql="select count(*) from student";
SQLiteDatabase database=db.getReadableDatabase();
Cursor cursor=database.rawQuery(sql, null);
cursor.moveToFirst();
return cursor.getInt(0);
}
}
需要注意的是getReadableDatabase和getWritableDatabase的用法,只是单纯的数据查询就使用getReadableDatabase,对数据进行修改操作就使用后者。另外需要注意sqlite的数据类型是没有任何作用的,只是为了程序员的互相合作。
4.编写android的Junit
package org.lxh.db;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.lxh.service.DBService;
import org.lxh.service.StudentService;
import org.lxh.vo.Student;
import android.test.AndroidTestCase;
import android.util.Log;
public class Test extends AndroidTestCase{
public void testSave(){
DBService db=new DBService(this.getContext());
db.getWritableDatabase();
}
public void testSaveStudent(){
StudentService service=new StudentService(this.getContext());
Student stu=null;
List<Student> stus=new ArrayList<Student>();
for(int i=0;i<10;i++){
stu = new Student();
stu.setUsername("陈亚峰"+i);
stus.add(stu);
}
service.savePerson(stus);
}
public void delete(){
StudentService service=new StudentService(this.getContext());
service.delete(11);
}
public void findAll(){
StudentService service=new StudentService(this.getContext());
List<Student> all=service.fiandAll();
Iterator<Student> it=all.iterator();
while(it.hasNext()){
Student stu=it.next();
Log.i("Test", "id:"+stu.getUserid()+","+"username"+stu.getUsername());
}
}
public void findRows(){
StudentService service=new StudentService(this.getContext());
Log.i("Test",String.valueOf(service.getCount()));
}
}
执行之后生成了数据,id为11的记录已经被删除了