sqlite应用【android菜鸟修行记(二)】8.30.2013

来源:互联网 发布:手机淘宝账号申请 编辑:程序博客网 时间:2024/04/30 09:22

欢迎关注我的微信公众账号“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}@Overridepublic void onCreate(SQLiteDatabase arg0) {// TODO Auto-generated method stubarg0.execSQL("create table imagetable (_id INTEGER PRIMARY KEY AUTOINCREMENT,image BLOB)");}@Overridepublic 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的构造方法中的几个参数说明:

public SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

contextto use to open or create the databasenameof the database file, or null for an in-memory databasefactoryto use for creating cursor objects, or null for the defaultversionnumber of the database (starting at 1); if the database is older, onUpgrade(SQLiteDatabase, int, int) will be used to upgrade the database

第三步 将图片转换为位图

Bitmap bitmap1=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

public static Bitmap decodeResource (Resources res, int id)

将现有的输入资源转换为位图

Parameters

resThe resources object containing the image dataidThe resource id of the image data
第四步 将位图转换为字节数组
int size=bitmap1.getWidth()*bitmap1.getHeight()*4; ByteArrayOutputStream baos=new ByteArrayOutputStream(size);  //构建一个字节数组输出流大小为sizebitmap1.compress(Bitmap.CompressFormat.PNG, 100, baos);<span></span>     //设置位图压缩格式为PNG,质量100%。输出流为baosbyte[] imagedata1=baos.toByteArray();<span></span>     //将字节数组输出流转换为字节数组
第五步 将字节数组保存到数据库中
ContentValues cv=new ContentValues();cv.put("_id", 1);cv.put("image", imagedata1);mydb.insert("imagetable", null, cv);iv1.setImageDrawable(null);try {baos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}

到此保存过程已经完成,可以在eclipse中的File explorer中查看数据库

红色便是我们保存数据库,点击后再点击绿色的按钮可以将数据库从虚拟机上导出到windows中,在利用SQLite Expert 可以查看数据库中的内容如图


查询数据时,过程相反:

Cursor cur=mydb.query("imagetable", new String[] {"_id","image"} , null, null, null, null, null);byte[] imagequery=null;if(cur.moveToNext()){imagequery=cur.getBlob(cur.getColumnIndex("image"));}Bitmap imagebitmap=BitmapFactory.decodeByteArray(imagequery, 0, imagequery.length);iv1.setImageBitmap(imagebitmap);


源码见:https://github.com/c123853648/android_saveImage1


原创粉丝点击