android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库故障

时间:2014-10-01 06:03:48

标签: android sqlite

我正在尝试修复一个不允许我打开数据库的错误。可能通过所有合并,出了点问题,但我似乎无法找到它。这是我的Log Cat报告。

    E/SQLiteLog(894): (14) cannot open file at line 30191 of [00bb9c9ce4]
 E/SQLiteLog(894): (14) os_unix.c:30191: (2) open(/data/data/com.packagename/databases/MainDB) - 
 Failed to open database '/data/data/com.packagename/databases/MainDB'.
 android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
    at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
    at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
    at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
    at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
    at com.packagename.database.DBAdapter$DatabaseHelper.checkDataBase(DBAdapter.java:757)
    at com.packagename.database.DBAdapter$DatabaseHelper.createDataBase(DBAdapter.java:734)
    at com.packagename.database.DBAdapter.resetAllData(DBAdapter.java:490)
    at com.packagename.OptionActivity$4.onClick(OptionActivity.java:157)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)

以下是代码的片段,它们可见(或相关)回到日志中。

选项活动

public void onClick_ResetAll(final View v) {
    ....
    ....
    @Override
    public void onClick(DialogInterface dialog, int which) {
        TrackUtils.sendClickTrack(OptionActivity.this, "ui_action", "button_press", "Resetall_yes");
        final DBAdapter dba = new DBAdapter(v.getContext());
        dba.open();
        dba.resetAllData(v.getContext());
        dba.close();

    }
    ....
    ....    
}

DBAdapter类

private DatabaseHelper mDbHelper;
private static SQLiteDatabase mDb;
@SuppressLint("SdCardPath")
private static String DB_PATH;
private static final String DATABASE_NAME = "MainDB";
private static final int DATABASE_VERSION = 3;
private final Context adapterContext;

public DBAdapter(Context context) {
    this.adapterContext = context;
    DB_PATH = adapterContext.getFilesDir().getAbsolutePath().replace("files", "databases") 
            + File.separator;
}

public DBAdapter open() throws SQLException {
    mDbHelper = new DatabaseHelper(adapterContext);

    try {
        mDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }

    try {
        mDbHelper.openDataBase();
    } catch (SQLException sqle) {
        throw sqle;
    }
    return this;
}

public void resetAllData(Context context) {
    context.deleteDatabase("MainDB");
    mDbHelper = new DatabaseHelper(adapterContext);

    try {
        mDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }

    try {
        mDbHelper.openDataBase();
    } catch (SQLException sqle) {
        throw sqle;
    }
}

DatabaseHelper(扩展SQLiteOpenHelper)

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        helperContext = context;
    }

    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();
        if (dbExist) {
        } else {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DATABASE_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY); // This is where the error apparently occurs
        } catch (SQLiteException e) {
        }
        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

我在这个网站上搜索过解决方案,但无济于事。

1 个答案:

答案 0 :(得分:16)

您在可能不存在的文件路径上使用SQLiteDatabase.openDatabase。在SQLiteDatabase.openDatabase调用

之前添加以下2行
File file = new File(myPath);    
if (file.exists() && !file.isDirectory())