SQLite数据库的onUpdate崩溃 [英] SQlite Database onUpdate crashes

查看:514
本文介绍了SQLite数据库的onUpdate崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的code更新从资产(我得到数据库的格式不正确)数据库之后导致应用程序崩溃。我知道问题出在哪里(因为表架构的变化),我需要选择*并在一个临时位置存储第一转储到新的数据库,但真的不知道如何实现这一点。能否请你帮我code?

目前,它是:

 公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
   尝试{
    copyDatabase();
   }赶上(IOException异常五){
    抛出新的错误(错误复制数据库+ e.toString());
   }
}公共无效copyDatabase()抛出IOException
   //打开本地数据库的输入流
   InputStream的myinput = myContext.getAssets()开(DB_NAME)。   //路径刚刚创建的空分贝
   字符串outfilename = DB_PATH + DB_NAME;   //打开空分贝的输出流
   的OutputStream myoutput =新的FileOutputStream(outfilename);   //传输字节到INPUTFILE到OUTPUTFILE
   字节[]缓冲区=新的字节[1024];
   INT长;
   而((长度= myinput.read(缓冲液))大于0){
       myoutput.write(缓冲液,0,长度);
   }   //关闭流
   myoutput.flush();
   myoutput.close();
   myinput.close();
}

BTW应用程序崩溃的第一个应用程序运行,当我再次打开它全部的寄托都工作正常。如果用户下载最新版本,而不必previous版本的问题不会同时出现。

logcat的:

  06-28 17:11:54.456:E / SQLiteLog(25926):(11)数据库在50987行腐败[00bb9c9ce4]
06-28 17:11:54.456:E / SQLiteLog(25926):(11)语句中止在4:从数字SELECT COUNT(*)]
06-28 17:11:54.456:E / DefaultDatabaseErrorHandler(25926):腐败报道的数据库SQLite的:/data/data/com.xxx.yyy/databases/database.db
06-28 17:11:54.456:E / DefaultDatabaseErrorHandler(25926):删除数据库文件:/data/data/com.xxx.yyy/databases/database.db
06-28 17:11:54.456:E / AndroidRuntime(25926):致命异常:主要
06-28 17:11:54.456:E / AndroidRuntime(25926):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.xxx.yyy / com.xxx.yyy.PuzzleActivity}:android.database.sqlite.SQLiteDatabaseCorruptException :数据库磁盘映像格式有误(code 11)


解决方案

当您从onUpgrade方法调用copyDatabase(),您可以有效地覆盖现有的数据库文件。问题是,该onUpgrade方法被调用,而在数据库打开和修改导致异常后。

该解决方案可以是设置一个标志在onUpgrade(像shouldUpgradeDatabaseFile =真)和外执行实际复制操作(例如,在getWritableDatabase()或getReadableDatabase())。

My current code causes app crash after updating database from assets (i get database is malformed). I know where is the problem (because the table schema change) and I need to SELECT * and dump into the new database by storing in a temp location first, but really don't know how to implement this. Could you please help me with code?

Currently it is:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   try {
    copyDatabase();
   } catch (IOException e) {
    throw new Error("Error copying database" + e.toString());
   }
}

public void copyDatabase() throws IOException {
   // 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 byte to inputfile to 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();
}

btw App crashes on first application run, when I open it again everthing works fine. The problem doesn't appear also if user downloads the newest version, without having previous version.

Logcat:

06-28 17:11:54.456: E/SQLiteLog(25926): (11) database corruption at line 50987 of     [00bb9c9ce4]
06-28 17:11:54.456: E/SQLiteLog(25926): (11) statement aborts at 4: [select count(*) from Numerical] 
06-28 17:11:54.456: E/DefaultDatabaseErrorHandler(25926): Corruption reported by sqlite on database: /data/data/com.xxx.yyy/databases/database.db
06-28 17:11:54.456: E/DefaultDatabaseErrorHandler(25926): deleting the database file: /data/data/com.xxx.yyy/databases/database.db
06-28 17:11:54.456: E/AndroidRuntime(25926): FATAL EXCEPTION: main
06-28 17:11:54.456: E/AndroidRuntime(25926): java.lang.RuntimeException: Unable to   start activity                ComponentInfo{com.xxx.yyy/com.xxx.yyy.PuzzleActivity}:     android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed (code 11)

解决方案

When you call copyDatabase() from the onUpgrade method, you are effectively overriding the existing database file. The problem is, that the onUpgrade method is called while the database is open, and modifying it causes an exception later.

The solution can be to set a flag in onUpgrade (like shouldUpgradeDatabaseFile = true) and execute the actual copy operation outside (e.g. in getWritableDatabase() or getReadableDatabase()).

这篇关于SQLite数据库的onUpdate崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆