如何在android中解决这个错误?

时间:2014-07-30 12:22:44

标签: android sqlite

  

我已经尝试在android中创建数据库,我得到了这个错误,我无法做到   理解为什么会发生这种错误,然后我检查代码,但我不能   修复,任何人都可以告诉我为什么会出现这个错误以及如何解决这个问题   错误。

07-30 12:03:52.144: E/SQLiteLog(5761): (14) cannot open file at line 30176 of [00bb9c9ce4] 07-30 12:03:52.174: E/SQLiteLog(5761): (14) os_unix.c:30176: (2) open(/data/data/com.implysoftwares.HotApps/databases/HotApps) - 07-30 12:03:52.315: E/SQLiteDatabase(5761): Failed to open database '/data/data/com.implysoftwares.HotApps/databases/HotApps'. 07-30 12:03:52.315: E/SQLiteDatabase(5761): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 07-30 12:03:52.315: E/SQLiteDatabase(5761): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) 07-30 12:03:52.315: E/SQLiteDatabase(5761): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209) 07-30 12:03:52.315: E/SQLiteDatabase(5761): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 07-30 12:03:52.315: E/SQLiteDatabase(5761): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 07-30 12:03:52.315: E/SQLiteDatabase(5761): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 07-30 12:03:52.315: E/SQLiteDatabase(5761): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 07-30 12:03:52.315: E/SQLiteDatabase(5761): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804) 07-30 12:03:52.315: E/SQLiteDatabase(5761): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789) 07-30 12:03:52.315: E/SQLiteDatabase(5761): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 07-30 12:03:52.315: E/SQLiteDatabase(5761): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)

SQLiteDatabase:

private static String DB_PATH = "/data/data/packagename/databases/";
private static String DB_Name = "HotApps";
private SQLiteDatabase myDataBase;
private final Context myContext;

public SQLitedatabase(Context context) {
    super(context, DB_Name, null, 1);
    this.myContext = 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 + DB_Name;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {
    InputStream myInput = myContext.getAssets().open(DB_Name);
    String outFileName = DB_PATH + DB_Name;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_Name;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READONLY);
}

public synchronized void close() {

    if (myDataBase != null)
        myDataBase.close();
    super.close();
}

2 个答案:

答案 0 :(得分:0)

openDatabase的第一个参数必须是数据库文件的完整路径,并且必须是您可以访问的目录。 确保路径和文件名正确无误,或使用ContextWrapper.getDatabasePath获取应用的路径。

更好的想法是使用openOrCreateDatabaseSQLiteOpenHelper,为您执行此操作。

答案 1 :(得分:0)

首先,您应该检查是否创建了数据库。

如果你有root设备,连接到PC并运行应用程序。否则,启动模拟器。

然后打开终端并输入

  

adb shell //连接到设备并打开文件系统//获取root cd   数据/数据/ you.package.name /数据库