sqlite应用【android菜鸟修行记(二)】8.30.2013
欢迎关注我的微信公众账号“90后萌呆小怪兽” 无组织,有纪律,爱创造,爱自由 “呆萌贱坏怪”是我们的style 让我们像小怪兽一样,用我们的方式,思维一起去颠覆这个世界吧
刚刚学习的android的数据存储教程中介绍了3种存储方法,一种是xml存储,一种是面向对象式的db4o数据库,一种是关系型数据库sqlite。作为初学者不太好总结三种数据库的优劣,在网上看到了有关前两种数据库的对比:【xml数据库与db4o的简要对比】http://www.cnblogs.com/chenxizhang/archive/2009/08/11/1543908.html xml存储是利用android中的SharePreferences方法将数据存储到xml文件中,可以存储boolean,String,float,long,int 5种数据类型存放位置为/data/data/<包名>/shared_prefs/存储的xml文件,一般用来存储字体大小,语言类型,游戏得分,登陆时间等。在eoe社区找到的一个demo:https://github.com/c123853648/android_xmlSave1 sqlite数据库是一种轻量级的关系型数据库,根据教程,今天写了一个demo。运行画面如下
第一步,实现SQLiteOpenHelper这个抽象类 public class MySQLiteOpenHelper extends SQLiteOpenHelper { public MySQLiteOpenHelper(Context context,String name,CursorFactory factory,int version) { super(context,name,factory,version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase arg0) { // TODO Auto-generated method stub arg0.execSQL("create table imagetable (_id INTEGER PRIMARY KEY AUTOINCREMENT,image BLOB)"); } @Override public void onUpgrade(SQLiteDatabase arg0,int arg1,int arg2) { // TODO Auto-generated method stub } }在onCreate方法中创建数据表imagetable。 第二步在主类中创建数据库“saveimage” <span> </span>mySQLiteOpenHelpe=new MySQLiteOpenHelper(this,"saveimage.db",null,1); <span> </span>mydb=mySQLiteOpenHelpe.getWritableDatabase();MySQLiteOpenHelper的构造方法中的几个参数说明: publicSQLiteOpenHelper(Contextcontext,Stringname,SQLiteDatabase.CursorFactoryfactory,int version) | context | to use to open or create the databasename | of the database file,or null for an in-memory databasefactory | to use for creating cursor objects,or null for the defaultversion | number of the database (starting at 1); if the database is older,res | The resources object containing the image dataid | The resource id of the image data
---|