数据库操作封装类-DBAdapter
public class DBAdapter {
private final Context context;
private SQLiteDatabase db;
private DBHelper dbHelper;
private final static String DB_TABLE="studentinfo";
private final static String DB_NAME="student.db";
private final static int DB_VERSION=1;
private final static String KEY_ID="id";
private final static String KEY_SNO="sno";
private final static String KEY_SNAME="sname";
private final static String KEY_CLASSES="classes";
public DBAdapter(Context _cContext) {
context = _cContext;
}
public void close()
{
if(db!=null)
{
db.close();
db=null;
}
}
public void open() throws SQLiteException
{
dbHelper = new DBHelper(context, DB_NAME, null, DB_VERSION);
try
{
db = dbHelper.getWritableDatabase();
}
catch(Exception e)
{
db = dbHelper.getReadableDatabase();
}
}
public long insert(Student s)
{
ContentValues cv = new ContentValues();
cv.put(KEY_SNO,s.getSno() );
cv.put(KEY_SNAME, s.getSname());
cv.put(KEY_CLASSES, s.getClasses());
return db.insert(DB_TABLE, null, cv);
}
public Student[] convertToStudent(Cursor c)
{
int resultsCount = c.getCount();
if(resultsCount==0||!c.moveToFirst())
{
return null;
}
Student []stu = new Student[resultsCount];
for (int i = 0; i < stu.length; i++) {
stu[i] = new Student();
String sno = c.getString(c.getColumnIndex("sno"));
String sname = c.getString(c.getColumnIndex("sname"));
String classes = c.getString(c.getColumnIndex("classes"));
stu[i].id=c.getInt(0);
stu[i].setSno(sno);
stu[i].setSname(sname);
stu[i].setClasses(classes);
c.moveToNext();
}
return stu;
}
private static class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
private static final String SQL="CREATE TABLE studentinfo ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "sno TEXT DEFAULT NONE,"
+ "sname TEXT DEFAULT NONE,"
+ "classes TEXT DEFAULT NONE"
+ ")";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+DB_TABLE);
onCreate(db);
}
}
}
实体对象类-Student
public class Student implements Serializable{
public int id=-1;
public String sno;
public String sname;
public String classes;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
@Override
public String toString() {
return "id:" + id +" " + "学号:" + sno +" "+ "姓名:" + sname +" "+ "班级:" + classes ;
}
}