java – onUpgrade数据库 – oldVersion – newVersion

前端之家收集整理的这篇文章主要介绍了java – onUpgrade数据库 – oldVersion – newVersion前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用这个DataBaseHelper.class,我被困在onUpgrade()方法上.我不知道如何弄清楚数据库的版本号是什么.我可以将版本设置为1,第一次发布它,当我发布更新时,我只需将版本设置为2(myDataBase.setVersion(2);).但只要应用程序正在运行,它就只有2.下次启动它再次为1.私有静态int DATABASE_VERSION也是如此.我在考虑将版本号存储在一个额外的表中,但在我看来,这似乎不是最佳实践.

那么如何确保版本号在升级增加并且保留它(分配给私有静态int DATABASE_VERSION或myDataBase.getVersion();)的值?

DataBaseHelper类:

  1. public class DataBaseHelper extends sqliteOpenHelper {
  2.  
  3. //The Android's default system path of your application database.
  4. private static String DB_PATH = "/data/data/com.mydatabase.db/databases/";
  5.  
  6. private static String DB_NAME = "database.sl3";
  7.  
  8. private sqliteDatabase myDataBase;
  9.  
  10. private final Context myContext;
  11.  
  12.  
  13. // Do you need this?
  14. private static int DATABASE_VERSION = 2;
  15. // or is this correct:
  16. // private static int DATABASE_VERSION = myDataBase.getVersion();
  17.  
  18.  
  19. /**
  20. * Constructor
  21. * Takes and keeps a reference of the passed context in order to access to
  22. * the application assets and resources.
  23. *
  24. * @param context
  25. */
  26. public DataBaseHelper(Context context) {
  27.  
  28. super(context,DB_NAME,null,DATABASE_VERSION);
  29. this.myContext = context;
  30. }
  31.  
  32. /**
  33. * Creates an empty database on the system and rewrites it with your own
  34. * database.
  35. * */
  36. public void
  37. createDataBase() throws IOException {
  38. boolean dbExist = checkDataBase();
  39.  
  40. if (dbExist) {
  41.  
  42.  
  43. }
  44. else {
  45.  
  46. //By calling this method and empty database will be created into the default system path
  47. //of your application so we are gonna be able to overwrite that database with our database.
  48.  
  49.  
  50. this.getWritableDatabase();
  51.  
  52.  
  53. try {
  54.  
  55. copyDataBase();
  56.  
  57. } catch (IOException e) {
  58.  
  59. throw new Error("Error copying database");
  60.  
  61. }
  62. }
  63.  
  64. }
  65.  
  66. /**
  67. * Check if the database already exist to avoid re-copying the file each
  68. * time you open the application.
  69. *
  70. * @return true if it exists,false if it doesn't
  71. */
  72. private boolean
  73. checkDataBase() {
  74.  
  75. sqliteDatabase checkDB = null;
  76.  
  77. try {
  78. String myPath = DB_PATH + DB_NAME;
  79. checkDB = sqliteDatabase.openDatabase(myPath,sqliteDatabase.OPEN_READWRITE);
  80.  
  81. } catch (sqliteException e) {
  82.  
  83. //database does't exist yet.
  84.  
  85. }
  86.  
  87. if (checkDB != null) {
  88.  
  89. checkDB.close();
  90.  
  91. }
  92. return checkDB != null ? true : false;
  93. }
  94.  
  95. /**
  96. * Copies your database from your local assets-folder to the just created
  97. * empty database in the
  98. * system folder,from where it can be accessed and handled.
  99. * This is done by transfering bytestream.
  100. * */
  101. private void
  102. copyDataBase() throws IOException {
  103.  
  104. //Open your local db as the input stream
  105. InputStream myInput = myContext.getAssets().open(DB_NAME);
  106.  
  107.  
  108. // Path to the just created empty db
  109. String outFileName = DB_PATH + DB_NAME;
  110.  
  111.  
  112. //Open the empty db as the output stream
  113. OutputStream myOutput = new FileOutputStream(outFileName);
  114.  
  115.  
  116. //transfer bytes from the inputfile to the outputfile
  117. byte[] buffer = new byte[1024];
  118. int length;
  119. while ((length = myInput.read(buffer)) > 0) {
  120. myOutput.write(buffer,length);
  121. }
  122.  
  123.  
  124. //Close the streams
  125. myOutput.flush();
  126. myOutput.close();
  127. myInput.close();
  128.  
  129. }
  130.  
  131. public void
  132. openDataBase() throws sqlException {
  133.  
  134. //Open the database
  135. String myPath = DB_PATH + DB_NAME;
  136. myDataBase = sqliteDatabase.openDatabase(myPath,sqliteDatabase.OPEN_READWRITE);
  137.  
  138.  
  139.  
  140. // Which parameters do I have to pass?
  141. onUpgrade(myDataBase,DATABASE_VERSION,2);
  142. }
  143.  
  144. @Override
  145. public synchronized void
  146. close() {
  147.  
  148. if (myDataBase != null)
  149. myDataBase.close();
  150.  
  151. super.close();
  152.  
  153. }
  154.  
  155. @Override
  156. public void
  157. onCreate(sqliteDatabase db) {
  158.  
  159. }
  160.  
  161. @Override
  162. public void
  163. onUpgrade( sqliteDatabase db,int oldVersion,int newVersion) {
  164.  
  165. Log.d ("onUpgrade first log",Integer.toString(myDataBase.getVersion()));
  166.  
  167.  
  168.  
  169.  
  170. if (oldVersion == 1) {
  171.  
  172. // do something
  173.  
  174.  
  175. // And then do this!?
  176. DATABASE_VERSION = 2;
  177. // or do this
  178. myDataBase.setVersion(2);
  179. Log.d ("onUpgrade sedond log",Integer.toString(myDataBase.getVersion()));
  180.  
  181. }
  182.  
  183. else {
  184. Log.d("onUpgrade","else-clause: Already upgraded!");
  185. }
  186.  
  187.  
  188.  
  189. }

解决方法

  1. // Do you need this?
  2. private static int DATABASE_VERSION = 2;

是的,你需要这个. (甚至更好,也是最终的.)

这告诉数据库助手最新版本的数据库模式是什么.这应该在您的应用程序代码中修复,并在您更改架构时递增.

当您的应用程序启动时,帮助程序会在运行时检查您的代码对最新版本的想法与上次创建或升级数据库时处于活动状态的版本相同. (这就是db.getVersion()的用途.)如果数字不匹配,那么帮助程序知道存储的数据库相对于应用程序代码已经过时,因此它运行升级例程.

看起来好像您不是从头开始创建数据库,而是从资产中导入现有数据库.当您执行此初始导入时,这是确保存储的版本与您的代码版本匹配的时间;或者将其直接应用于资产中的数据库文件,或者,如果您确定资产中的数据库文件代码匹配,则调用setVersion(DATABASE_VERSION).

在任何情况下,您都不应该尝试修改onUpgrade()例程中的版本号.只有在版本不匹配时才会调用它,并且您在此处应该做的就是进行任何更改以使数据库保持最新.升级完成后,帮助程序将管理新版本号的存储.

猜你在找的Java相关文章