网站 代理 备案 费用,渭南seo快速排名,做设计接外快在哪个网站,关键词指数查询工具转自#xff1a;http://blog.csdn.net/leehong2005/article/details/9128501 请考虑如下情况#xff1a; 在数据库升级时#xff0c;不同版本的数据库#xff0c;他们定义的表结构完全可能是不一样的#xff0c;比如V1.0的表A有10个column#xff0c;而在V1.1的表A有12个… 转自http://blog.csdn.net/leehong2005/article/details/9128501 请考虑如下情况 在数据库升级时不同版本的数据库他们定义的表结构完全可能是不一样的比如V1.0的表A有10个column而在V1.1的表A有12个colum在升级时表A增加了两列此时我们应该怎么做呢。 总体思路 1将表A重命名改了A_temp。 2创建新表A。 3将表A_temp的数据插入到表A。 下面代码列出了更新表的实现upgradeTables给定表名更新的列名就可以实现数据库表的更新。 [java] view plaincopy /** * 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。 [java] view plaincopy 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.0V2.0当用户安装新版本的程序后必须要保证用户数据不能丢失对于数据库设计如果发生变更如多添加一张表表的字段增加或减少等那么我们必须想好数据库的更新策略。 1定义数据库版本 数据库的版本是一个整型值在创建SQLiteOpenHelper时会传入该数据库的版本如果传入的数据库版本号比数据库文件中存储的版本号大的话那么SQLiteOpenHelper#onUpgrade()方法就会被调用我们的升级应该在该方法中完成。 2如何写升级逻辑 假如我们开发的程序已经发布了两个版本V1.0V1.2我们正在开发V1.3。每一版的数据库版本号分别是181920。 对于这种情况我们应该如何实现升级 用户的选择有 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()方法的实现如下 [java] view plaincopy // 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()方法中删除表时注意使用事务处理使得修改能立即反应到数据库文件中。 SQL语句 由于Android是使用开源的SQLite3作为其数据库所以我们在开发数据库模块时一定要注意SQLite3支持哪些关键字函数等不是所有的关键字SQLite都是支持的。 下面列出了一些参考链接 SQLite3官方文档http://sqlite.org/ W3CSchool网站http://www.w3school.com.cn/sql/index.asp/ SQL语句写得好坏能直接影响到数据库的操作。我曾经就遇到过SQL语句影响查询性能更新3000条记录用时30移左右但在对WHERE条件的字段加上索引后性能提升到3~4秒。 参考http://androidll.iteye.com/blog/1570943