标签:exists -- cat nsa tables table rri database catch
升级:重写onUpgrade方法
降级
onDowngrade()数据库降级:比如从数据库4降级到数据库3必须重写该方法。
@Override public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { super.onDowngrade(db, oldVersion, newVersion); }
迁移数据:
protected void upgradeTables(SQLiteDatabase db, String tableName, String columns) { try { db.beginTransaction(); // 1, 将现有表重命名为临时表 String tempTableName = tableName + "_temp"; String sql = "ALTER TABLE " + tableName +" RENAME TO " + tempTableName; execSQL(db, sql, null); // 2, 创建新表 //onCreateTable(db); createNewTableX(db); // 3, 将临时表数据导入新表中 sql = "INSERT INTO " + tableName + " (" + columns + ") " + " SELECT " + columns + " FROM " + tempTableName; execSQL(db, sql, null); // 4, 删除临时表 execSQL(db, "DROP TABLE IF EXISTS " + tempTableName, null); db.setTransactionSuccessful(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); } }
标签:exists -- cat nsa tables table rri database catch
原文地址:http://www.cnblogs.com/androidsuperman/p/7041460.html