2012-03-31 72 views
0

我的应用程序使用了一个sqlite数据库,该数据库是从assets文件夹预先创建并在运行时复制的。它通常工作得很好。 我已经recenetly添加到数据库的新列,并增加了数据库版本,以便onUpgrade方法运行。这一切运作良好,但新的数据库复制没有新增列。这是onUpgrade代码:Android Sqlite onUpgrade不会复制新列

//Open your local db as the input stream 
         InputStream myInput = myContext.getAssets().open(DB_NAME); 

         // Path to the just created empty db 
         String outFileName = DB_PATH + DB_NAME; 

         //Open the empty db as the output stream 
         OutputStream myOutput = new FileOutputStream(outFileName); 

         //transfer bytes from the inputfile to the outputfile 
         byte[] buffer = new byte[1024]; 
         int length; 
         while ((length = myInput.read(buffer))>0){ 
          myOutput.write(buffer, 0, length); 
         } 

         //Close the streams 
         myOutput.flush(); 
         myOutput.close(); 
         myInput.close(); 

任何想法如何解决它?

10X!

+1

我建议你考虑切换到['SQLiteAssetHelper'](https://github.com/jgilfelt/android-sqlite-asset-helper),它已经完成了所有这些工作并进行了调试。 – CommonsWare 2012-03-31 22:56:31

+0

乍一看似乎很有趣,我会试一试并回报:) 10X! – 2012-03-31 23:23:43

+0

试试这个: http://stackoverflow.com/questions/9109438/using-already-created-database-with-android/9109728#9109728 – 2012-04-01 00:19:01

回答

0

有时候显然需要问。 :)

您确定已将新的数据库复制到您的资产文件夹,以便它可以再次移入数据库目录吗?

+0

是的,我做了它,10X :) – 2012-04-10 23:06:37