SQLiteDatabaseCorruptException:数据库磁盘映像格式不正确 [英] SQLiteDatabaseCorruptException: database disk image is malformed

查看:1550
本文介绍了SQLiteDatabaseCorruptException:数据库磁盘映像格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与我的这一个程序碰壁。我在网上搜索周围的解决这个问题,但我一直没能找到一个所以这里去..

I've run into a wall with my program on this one. I've searched around the internet for a solution to this problem but I haven't been able to find one so here goes..

我想从我的远程服务器上下载SQLite数据库。这似乎是下载好,我可以拉文件关闭手机并查看它。它看起来不错。然而,当我的程序尝试读取我得到的数据库:

I am trying to download a SQLite database from my remote server. It seems to download fine, i can pull the file off the phone and view it. It looks fine. However, when my program tries to read the database I get:

android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed

所以我删除了数据库的服务器上,并在手机上的数据库,我跳到是应该创建数据库,并将其提交到服务器的活动。它这样做就好了,现在数据库已经在手机和服务器上重新创建。我可以打开他们,他们看起来很好。

So I delete the db on the server, and the db on the phone and I skip to the activity that is supposed to create the database and submit it to the server. It does this just fine, now the database has been re-created on the phone and server. I can open them and they look fine.

我重新启动程序,我的微调不填充数据,我得到同样的错误。

I restart the program and my spinner is not filling with the data and I am getting the same error.

这里是code,它充满着微调。

here is the code that is filling the spinner.

    private void fillSpinner(){
    mDbHelper = new myDbAdapter(this);
    mDbHelper.open();
    Cursor c = mDbHelper.fetchColumns(new String[] {"_id","name"});

    if ( ! c.moveToFirst() ){

        mDbHelper.createRow("Create Name");
        c = mDbHelper.fetchColumns(new String[] {"_id","name"});

    }


    // create an array to specify which fields we want to display
    String[] from = new String[]{"name"};
    // create an array of the display item we want to bind our data to
    int[] to = new int[]{android.R.id.text1};
    // create simple cursor adapter
    SimpleCursorAdapter adapter =
        new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
    mDbHelper.close();

    adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    // get reference to our spinner
    c.close();
    s.setAdapter(adapter);

}

这是fetchColumns方法,以供参考,因为它是用来尝试并访问数据库中的数据。

This is the fetchColumns method, for reference since it is used to try and access the data in the database.

    public Cursor fetchColumns(String[] colnames) {
    Cursor mCursor = database.query(DATABASE_TABLE, colnames, null, 
            null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

的logcat指向我在此方法的方向。以上几点提到getWritableDatabase()作为罪魁祸首的错误。该数据库不为空,我可以用一个SQLite数据库浏览器中查看其内容。

Logcat points me in the direction of this method. the error mentioned above points to getWritableDatabase() as the culprit. The db is not null, i can view the contents with an sqlite database browser.

    public SQLiteDatabase open() throws SQLException {
    dbHelper = new myDbHelper(context);
    database = dbHelper.getWritableDatabase();

    return database;
}

下面是下载数据库的方法。

Here is the method that downloads the db.

    public void downloadFile(String file_name) throws IOException {

    URL url = new URL(file_url + file_name);
    URLConnection connection = url.openConnection();
    InputStream response = connection.getInputStream();

    FileOutputStream fos = new FileOutputStream(local_file_path + file_name);
    byte data[] = new byte[1024];
    fos.write(data);
    fos.close();
    response.close();
}

任何人有什么可能会造成这个问题的任何想法吗?我已经调试和堆栈跟踪,这一切似乎是从open()方法来了。这里是堆栈跟踪

Anyone have any ideas about what could be causing the problem? i've debugged and stack traced and it all seems to be coming from the open() method. Here is the Stack trace

    11-05 19:39:32.861: INFO/Database(9084): sqlite returned: error code = 11, msg = 
            database corruption found by source line 40107
    11-05 19:39:32.861: INFO/Database(9084): sqlite returned: error code = 11, msg =         database disk image is malformed
    11-05 19:39:32.861: ERROR/Database(9084): CREATE TABLE android_metadata failed
    11-05 19:39:32.881: ERROR/Database(9084): Failed to setLocale() when constructing, closing the database
    11-05 19:39:32.881: ERROR/Database(9084): android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed
    11-05 19:39:32.881: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1950)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1818)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
    11-05 19:39:32.881: ERROR/Database(9084):     at com.project.myDbAdapter.open(myDbAdapter.java:42)
    11-05 19:39:32.881: ERROR/Database(9084):     at com.project.Main.fillSpinner(Main.java:77)
    11-05 19:39:32.881: ERROR/Database(9084):     at com.project.Main.onCreate(Main.java:40)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.os.Handler.dispatchMessage(Handler.java:99)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.os.Looper.loop(Looper.java:123)
    11-05 19:39:32.881: ERROR/Database(9084):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    11-05 19:39:32.881: ERROR/Database(9084):     at java.lang.reflect.Method.invokeNative(Native Method)
    11-05 19:39:32.881: ERROR/Database(9084):     at java.lang.reflect.Method.invoke(Method.java:521)
    11-05 19:39:32.881: ERROR/Database(9084):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    11-05 19:39:32.881: ERROR/Database(9084):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    11-05 19:39:32.881: ERROR/Database(9084):     at dalvik.system.NativeStart.main(Native Method)
    11-05 19:39:32.901: ERROR/Database(9084): Deleting and re-creating corrupt database /data/data/com.project/databases/names
    11-05 19:39:32.901: ERROR/Database(9084): android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed
    11-05 19:39:32.901: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1950)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1818)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
    11-05 19:39:32.901: ERROR/Database(9084):     at com.project.myDbAdapter.open(myDbAdapter.java:42)
    11-05 19:39:32.901: ERROR/Database(9084):     at com.project.Main.fillSpinner(SBMain.java:77)
    11-05 19:39:32.901: ERROR/Database(9084):     at com.project.Main.onCreate(SBMain.java:40)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.os.Handler.dispatchMessage(Handler.java:99)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.os.Looper.loop(Looper.java:123)
    11-05 19:39:32.901: ERROR/Database(9084):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    11-05 19:39:32.901: ERROR/Database(9084):     at java.lang.reflect.Method.invokeNative(Native Method)
    11-05 19:39:32.901: ERROR/Database(9084):     at java.lang.reflect.Method.invoke(Method.java:521)
    11-05 19:39:32.901: ERROR/Database(9084):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    11-05 19:39:32.901: ERROR/Database(9084):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    11-05 19:39:32.901: ERROR/Database(9084):     at dalvik.system.NativeStart.main(Native Method)
    11-05 19:39:33.311: INFO/ActivityManager(66): Displayed activity com.project.Main: 552 ms (total 552 ms)

任何帮助是极大的AP preciated!在此先感谢!

Any help is greatly appreciated!! Thanks in advance!

下面是工作code

      public void downloadFile(String file_name) throws IOException {

          URL url = new URL(file_url + file_name);
        URLConnection connection = url.openConnection();
        InputStream response = connection.getInputStream();

        FileOutputStream fos = new FileOutputStream(local_file_path + file_name);
         byte data[] = new byte[1024];

         IOUtils.copy(response, fos);
        response.close();
       }

    private void fillSpinner(){
    mDbHelper = new myDbAdapter(this);
    mDbHelper.open();
    Cursor c = mDbHelper.fetchColumns(new String[] {"_id","name"});

    if ( ! c.moveToFirst() ){

    mDbHelper.createRow("Create Name");
    c = mDbHelper.fetchColumns(new String[] {"_id","name"});

    c.close();
    }


    // create an array to specify which fields we want to display
    String[] from = new String[]{"name"};
    // create an array of the display item we want to bind our data to
    int[] to = new int[]{android.R.id.text1};
    // create simple cursor adapter
    SimpleCursorAdapter adapter =
    new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
    mDbHelper.close();

    adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    // get reference to our spinner
    c.close();
    s.setAdapter(adapter);

    }

我不关闭光标。

I wasn't closing the cursor.

推荐答案

是啊。你不实际复制该文件。

Yeah. You're not actually copying the file.

public void downloadFile(String file_name) throws IOException {

    URL url = new URL(file_url + file_name);
    URLConnection connection = url.openConnection();
    InputStream response = connection.getInputStream();

    FileOutputStream fos = new FileOutputStream(local_file_path + file_name);
    byte data[] = new byte[1024];

    //WHERE'S THE BEEF?

    fos.write(data);
    fos.close();
    response.close();
}

另外,手动编写一份code是容易出错。我做了Apache的百科全书的下调版本:

Also, manually writing that copy code is error prone. I made a trimmed down version of apache commons:

http://www.touchlab.co/blog/android-mini-commons /

呼叫

IOUtils.copy(response, fos);

这篇关于SQLiteDatabaseCorruptException:数据库磁盘映像格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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