本文介绍了android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to fix an error which isn't allowing me to open the database. Possibly through all the merges, something went wrong but I can't seem to find it. Here is my Log Cat report.

    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)

这是在日志中可见(或相关)的代码片段.

And here are snippets of the code which is visible (or related) back to the log.

选项活动

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;
    }

我已经在此网站上搜索解决方案,但无济于事.

I have searched for solutions on this website but to no avail.

推荐答案

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

You are using SQLiteDatabase.openDatabase on a file path that may not exist. Add below 2 lines just before SQLiteDatabase.openDatabase call

File file = new File(myPath);
if (file.exists() && !file.isDirectory())

这篇关于android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:57