首页 文章 精选 留言 我的

精选列表

搜索[android studio],共10000篇文章
优秀的个人博客,低调大师

Android 数据库升级解决方案

请考虑如下情况: 在数据库升级时,不同版本的数据库,他们定义的表结构完全可能是不一样的,比如V1.0的表A有10个column,而在V1.1的表A有12个colum,在升级时,表A增加了两列,此时我们应该怎么做呢。 总体思路 1,将表A重命名,改了A_temp。 2,创建新表A。 3,将表A_temp的数据插入到表A。 下面代码列出了更新表的实现,upgradeTables,给定表名,更新的列名,就可以实现数据库表的更新。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 /** * Upgrade tables. In this method, the sequence is: * <b> * <p>[1] Rename the specified table as a temporary table. * <p>[2] Create a new table which name is the specified name. * <p>[3] Insert data into the new created table, data from the temporary table. * <p>[4] Drop the temporary table. * </b> * * @param db The database. * @param tableName The table name. * @param columns The columns range, format is "ColA, ColB, ColC, ... ColN"; */ protected void upgradeTables(SQLiteDatabase db, String tableName, String columns) { try { db.beginTransaction(); // 1, Rename table. String tempTableName = tableName + "_temp" ; String sql = "ALTER TABLE " + tableName + " RENAME TO " + tempTableName; execSQL(db, sql, null ); // 2, Create table. onCreateTable(db); // 3, Load data sql = "INSERT INTO " + tableName + " (" + columns + ") " + " SELECT " + columns + " FROM " + tempTableName; execSQL(db, sql, null ); // 4, Drop the temporary table. execSQL(db, "DROP TABLE IF EXISTS " + tempTableName, null ); db.setTransactionSuccessful(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); } } 得到数据库表的列名 我们可以通过SQL表得到表的列名。 这里需要注意的一点,int columnIndex = c.getColumnIndex("name"); 这里根据name去取得index。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 protected String[] getColumnNames(SQLiteDatabase db, String tableName) { String[] columnNames = null ; Cursor c = null ; try { c = db.rawQuery( "PRAGMA table_info(" + tableName + ")" , null ); if ( null != c) { int columnIndex = c.getColumnIndex( "name" ); if (- 1 == columnIndex) { return null ; } int index = 0 ; columnNames = new String[c.getCount()]; for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { columnNames[index] = c.getString(columnIndex); index++; } } } catch (Exception e) { e.printStackTrace(); } finally { closeCursor(c); } return columnNames; } upgradeTables方法应该是在onUpgrade方法中去调用。 数据库升级的意义 在应用程序开发的过程中,数据库的升级是一个很重要的组成部分(如果用到了数据库),因为程序可能会有V1.0,V2.0,当用户安装新版本的程序后,必须要保证用户数据不能丢失,对于数据库设计,如果发生变更(如多添加一张表,表的字段增加或减少等),那么我们必须想好数据库的更新策略。 1,定义数据库版本 数据库的版本是一个整型值,在创建SQLiteOpenHelper时,会传入该数据库的版本,如果传入的数据库版本号比数据库文件中存储的版本号大的话,那么SQLiteOpenHelper#onUpgrade()方法就会被调用,我们的升级应该在该方法中完成。 2,如何写升级逻辑 假如我们开发的程序已经发布了两个版本:V1.0,V1.2,我们正在开发V1.3。每一版的数据库版本号分别是18,19,20。 对于这种情况,我们应该如何实现升级? 用户的选择有: 1)V1.0 -> V1.3 DB 18 -> 20 2)V1.1 -> V1.3 DB 19 -> 20 3,注意 数据库的每一个版本所代表的数据库必须是定义好的,比如说V18的数据库,它可能只有两张表TableA和TableB,如果V19要添加一张表TableC,如果V20要修改TableC,那么每一个版本所对应的数据库结构如下: V18 ---> TableA, TableB V19 ---> TableA, TableB, TableC V20 ---> TableA, TableB, TableC (变更) onUpgrade()方法的实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // Pattern for upgrade blocks: // // if (upgradeVersion == [the DATABASE_VERSION you set] - 1){ // .. your upgrade logic.. // upgradeVersion = [the DATABASE_VERSION you set] // } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { int upgradeVersion = oldVersion; if ( 18 == upgradeVersion) { // Create table C String sql = "CREATE TABLE ..." ; db.execSQL(sql); upgradeVersion = 19 ; } if ( 20 == upgradeVersion) { // Modify table C upgradeVersion = 20 ; } if (upgradeVersion != newVersion) { // Drop tables db.execSQL( "DROP TABLE IF EXISTS " + tableName); // Create tables onCreate(db); } } 从上面的代码可以看到,我们在onUpgrade()方法中,处理了数据库版本从18 -> 20的升级过程,这样做的话,不论用户从18 -> 20,还是从19 -> 20,最终程序的数据库都能升级到V20所对应的数据库结构。 4,如何保证数据不丢失 这是很重要的一部分,假设要更新TableC表,我们建议的做法是: 1)将TableC重命名为TableC_temp SQL语句可以这样写:ALERT TABLE TableC RENAME TO TableC_temp; 2)创建新的TableC表 3)将数据从TableC_temp中插入到TableC表中 SQL语句可以这样写:INSERT INTO TableC (Col1, Col2, Col3) SELECT (Col1, Col2, Col3) FROM TableC_temp; 经过这三步,TableC就完成了更新,同时,也保留了原来表中的数据。 注意: 在onUpgrade()方法中,删除表时,注意使用事务处理,使得修改能立即反应到数据库文件中。 本文转自java豆子博客园博客,原文链接:http://www.cnblogs.com/error404/p/3738630.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android中Bitmap,byte[],Drawable相互转化

一、相关概念 1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象 2、Canvas画布,绘图的目的区域,用于绘图 3、Bitmap位图,用于图的处理 4、Matrix矩阵 二、Bitmap 1、从资源中获取Bitmap Resourcesres=getResources(); Bitmapbmp=BitmapFactory.decodeResource(res,R.drawable.icon); 2、Bitmap → byte[] []Bitmap2Bytes(Bitmapbm){ ByteArrayOutputStreambaos=ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG,100,baos); baos.toByteArray(); } 3、byte[] → Bitmap BitmapBytes2Bimap([]b){ (b.length!=0){ BitmapFactory.decodeByteArray(b,0,b.length); }{ ; } } 4、Bitmap缩放 BitmapzoomBitmap(Bitmapbitmap,width,height){ w=bitmap.getWidth(); h=bitmap.getHeight(); Matrixmatrix=Matrix(); scaleWidth=(()width/w); scaleHeight=(()height/h); matrix.postScale(scaleWidth,scaleHeight); Bitmapnewbmp=Bitmap.createBitmap(bitmap,0,0,w,h,matrix,); newbmp; } 5、将Drawable转化为Bitmap BitmapdrawableToBitmap(Drawabledrawable){ w=drawable.getIntrinsicWidth(); h=drawable.getIntrinsicHeight(); Bitmap.Configconfig=drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888 :Bitmap.Config.RGB_565; Bitmapbitmap=Bitmap.createBitmap(w,h,config); Canvascanvas=Canvas(bitmap); drawable.setBounds(0,0,w,h); drawable.draw(canvas); bitmap; } 6、获得圆角图片 BitmapgetRoundedCornerBitmap(Bitmapbitmap,roundPx){ w=bitmap.getWidth(); h=bitmap.getHeight(); Bitmapoutput=Bitmap.createBitmap(w,h,Config.ARGB_8888); Canvascanvas=Canvas(output); color=0xff424242; Paintpaint=Paint(); Rectrect=Rect(0,0,w,h); RectFrectF=RectF(rect); paint.setAntiAlias(); canvas.drawARGB(0,0,0,0); paint.setColor(color); canvas.drawRoundRect(rectF,roundPx,roundPx,paint); paint.setXfermode(PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap,rect,rect,paint); output; } 7、获得带倒影的图片 BitmapcreateReflectionImageWithOrigin(Bitmapbitmap){ reflectionGap=4; w=bitmap.getWidth(); h=bitmap.getHeight(); Matrixmatrix=Matrix(); matrix.preScale(1,-1); BitmapreflectionImage=Bitmap.createBitmap(bitmap,0,h/2,w, h/2,matrix,); BitmapbitmapWithReflection=Bitmap.createBitmap(w,(h+h/2), Config.ARGB_8888); Canvascanvas=Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap,0,0,); PaintdeafalutPaint=Paint(); canvas.drawRect(0,h,w,h+reflectionGap,deafalutPaint); canvas.drawBitmap(reflectionImage,0,h+reflectionGap,); Paintpaint=Paint(); LinearGradientshader=LinearGradient(0,bitmap.getHeight(),0, bitmapWithReflection.getHeight()+reflectionGap,0x70ffffff, 0x00ffffff,TileMode.CLAMP); paint.setShader(shader); paint.setXfermode(PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0,h,w,bitmapWithReflection.getHeight() +reflectionGap,paint); bitmapWithReflection; } 三、Drawable 1、Bitmap转换成Drawable Bitmapbm=xxx;BitmapDrawablebd=BitmapDrawable(getResource(),bm); 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。 2、Drawable缩放 DrawablezoomDrawable(Drawabledrawable,w,h){ width=drawable.getIntrinsicWidth(); height=drawable.getIntrinsicHeight(); Bitmapoldbmp=drawableToBitmap(drawable); Matrixmatrix=Matrix(); sx=(()w/width); sy=(()h/height); matrix.postScale(sx,sy); Bitmapnewbmp=Bitmap.createBitmap(oldbmp,0,0,width,height, matrix,); BitmapDrawable(newbmp); } 本文转自dyh7077063的博客http://dyh7077063.iteye.com/blog/970672 本文转自wauoen51CTO博客,原文链接:http://blog.51cto.com/7183397/1604493,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码