在andorid端使用sqlite数据库是经常的事,通常来说都是对每个属性对应一个字段,然后分字段的来读取,但是今天我要说的不是这样的。我们通过对象序列化来存取。因为一个好的程序员总是得学会偷懒。
Demo效果
好了看代码吧。
我们首先还是新建一个对象,然后我们就对这个对象存取。
public class Student implements Serializable { /** * */ private static final long serialVersionUID = 1L; String name; int age; public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }然后是数据库的连接类
/*** * 单例模式实现数据库连接 * @author bobo * */ public class Dbhelper extends SQLiteOpenHelper { private static Dbhelper dbhelper = null; public static Dbhelper getInstens(Context context) { if (dbhelper == null) { dbhelper = new Dbhelper(context); } return dbhelper; } private Dbhelper(Context context) { super(context, "datebase.db", null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub //这张表采用二进制文件存储对象注意第二个字段我们将对象存取在这里面 String sql_class_table="create table if not exists classtable(_id integer primary key autoincrement,classtabledata text)"; db.execSQL(sql_class_table); } @Override public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) { // TODO Auto-generated method stub } }最后是一个数据库操作类,最主要的方法在这个类里面,通过二进制流把对象转换为对象流然后取出二进制存取进数据库,最后取的时候也一样。首先取出二进制对象,然后通过流还原对象。
/** * 数据库操作 * @author bobo * */ public class StudentServcie { Context context; public StudentServcie(Context context) { // TODO Auto-generated constructor stub this.context = context; } /** * 保存 * @param student */ public void saveObject(Student student) { ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); try { ObjectOutputStream objectOutputStream = new ObjectOutputStream(arrayOutputStream); objectOutputStream.writeObject(student); objectOutputStream.flush(); byte data[] = arrayOutputStream.toByteArray(); objectOutputStream.close(); arrayOutputStream.close(); Dbhelper dbhelper = Dbhelper.getInstens(context); SQLiteDatabase database = dbhelper.getWritableDatabase(); database.execSQL("insert into classtable (classtabledata) values(?)", new Object[] { data }); database.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Student getObject() { Student student = null; Dbhelper dbhelper = Dbhelper.getInstens(context); SQLiteDatabase database = dbhelper.getReadableDatabase(); Cursor cursor = database.rawQuery("select * from classtable", null); if (cursor != null) { while (cursor.moveToNext()) { byte data[] = cursor.getBlob(cursor.getColumnIndex("classtabledata")); ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(data); try { ObjectInputStream inputStream = new ObjectInputStream(arrayInputStream); student = (Student) inputStream.readObject(); inputStream.close(); arrayInputStream.close(); break;//这里为了测试就取一个数据 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return student; } }同样我们也可以对list,或者map对象进行保存,方法跟上面的一样,但是map或者list里面的对象必须序列化。
最后给出测试类。
public class MainActivity extends Activity { StudentServcie servcie; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); servcie = new StudentServcie(this); } public void onclick(View view) { switch (view.getId()) { case R.id.save: servcie.saveObject(new Student("李雷", 20)); Toast.makeText(this, "存取完成", 0).show(); break; case R.id.load: Student student = servcie.getObject(); if(student!=null) Toast.makeText(this, student.getName()+student.getAge(), 0).show(); break; } } }
android sqlite另类用法(对象存取),布布扣,bubuko.com
原文:http://blog.csdn.net/codebob/article/details/37091003