热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SQLiteDiskIOException:创建数据库时的磁盘I/O错误。-SQLiteDiskIOException:diskI/Oerrorwhencreatingdatabase

Iamcreatingadatabaseinandroidusingthiscode:我正在使用这段代码在android上创建一个数据库:publicclassPackag

I am creating a database in android using this code:

我正在使用这段代码在android上创建一个数据库:

public class PackageDBHelper extends SQLiteOpenHelper{

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.myapp/databases/";

private static String DB_NAME = "my_db.db";

public SQLiteDatabase db; 

private final Context myContext;

public PackageDBHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myCOntext= context;
}   
public void createDataBase() throws IOException{

    boolean dbExist = checkDataBase();

    if(dbExist){
        //do nothing - database already exist
    }else{

        //By calling this method and empty database will be created into the default system path
           //of your application so we are gonna be able to overwrite that database with our database.
        this.getReadableDatabase();  //error occurs here

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

StackTrace:

加:

android.database.sqlite.SQLiteDiskIOException: disk I/O error
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1668)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1684)
at android.app.ActivityThread.access$1500(ActivityThread.java:124)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:948)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3717)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteDiskIOException: disk I/O error
at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1849)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:197)
at com.myapp.util.PackageDBHelper.createDataBase(PackageDBHelper.java:51)

This app is on the market and I have received a few error reports. I have not been able to duplicate it on any of my devices, and the error is relatively rare. I realize the DB_PATH should probably be retrieved from the device, however that should not be effecting the call to getReadableDatabase().

这个app在市场上,我收到了一些错误报告。我没有能够在我的任何设备上复制它,而且这个错误是相对少见的。我知道应该从设备中检索DB_PATH,但是不应该影响到getReadableDatabase()的调用。

Any ideas?

什么好主意吗?

2 个解决方案

#1


5  

I had the same problem with HTC Desire HD, and Galaxy S on Android 2.1

我对HTC Desire HD和Android 2.1的Galaxy S也有同样的问题。

Using the following as for DB_PATH seems to fix the problem:

对于DB_PATH,使用以下方法似乎可以解决问题:

Environment.getDataDirectory() + “/data/YOUR_PACKAGE/databases/”;

More details available here https://stackoverflow.com/a/4828540

这里有更多的详细信息,https://stackoverflow.com/a/4828540。

#2


0  

try changing

试着改变

public PackageDBHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myCOntext= context;
} 

to

public PackageDBHelper(Context context) {

    super(context, "my_db", null, 1);//without the extension .db
    this.myCOntext= context;
} 

推荐阅读
author-avatar
美玲珍任欢治
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有