Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SQLiteDatabaseCorruptException:数据库磁盘映像格式不正确_Java_Android_Sqlite - Fatal编程技术网

Java SQLiteDatabaseCorruptException:数据库磁盘映像格式不正确

Java SQLiteDatabaseCorruptException:数据库磁盘映像格式不正确,java,android,sqlite,Java,Android,Sqlite,这一次我的程序遇到了麻烦。我已经在互联网上搜索了这个问题的解决方案,但是我还没有找到,所以就这样 我正在尝试从远程服务器下载SQLite数据库。它似乎下载得很好,我可以从手机上取下文件并查看它。看起来不错。但是,当我的程序尝试读取数据库时,我得到: android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed 因此,我删除了服务器上的数据库和电话上的数据库,然后跳到创建数据库并将

这一次我的程序遇到了麻烦。我已经在互联网上搜索了这个问题的解决方案,但是我还没有找到,所以就这样

我正在尝试从远程服务器下载SQLite数据库。它似乎下载得很好,我可以从手机上取下文件并查看它。看起来不错。但是,当我的程序尝试读取数据库时,我得到:

android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed
因此,我删除了服务器上的数据库和电话上的数据库,然后跳到创建数据库并将其提交给服务器的活动。它做得很好,现在数据库已经在手机和服务器上重新创建。我可以打开它们,它们看起来很好

我重新启动程序,我的微调器没有填充数据,我得到了相同的错误

下面是填充微调器的代码

    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方法,仅供参考,因为它用于尝试访问数据库中的数据

    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数据库浏览器查看内容

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

    return database;
}
下面是下载数据库的方法

    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()方法。这是堆栈跟踪

    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)

我没有关闭光标。

是的。你并不是在复制文件

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();
}
此外,手动编写复制代码容易出错。我制作了apache commons的精简版:

召唤


好的,我添加了行并取出了fos.write(数据)。似乎没有任何错误,并且文件正在复制,我可以拉它并查看条目,但微调器没有填充?:(我自己从来没有使用过带光标适配器的微调器。我帮不了什么忙。太棒了,已经修复。谢谢你的帮助。发布了一个使用工作代码Sweet的编辑!我想你是“迷你公共空间”libs的用户2。只是想一想。最后调用“fos.close()”,以确保输出流被刷新,并且不保留打开的文件句柄。
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();
}
IOUtils.copy(response, fos);