Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.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
Android 获取SQLiteCantOpenDatabaseException_Android_Sqlite_Android Sqlite - Fatal编程技术网

Android 获取SQLiteCantOpenDatabaseException

Android 获取SQLiteCantOpenDatabaseException,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,在我的应用程序中,我的资产文件夹中有一个SQLite数据库的副本。据我所知,它运转良好。当我的应用程序第一次在emulator中安装时,会出现如下错误: 下面是我尝试过的另一种方法: checkDB = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.OPEN_READONLY); 我也试着用这个: SQLiteDatabase.openDatabase(mPath, null,SQLiteDat

在我的应用程序中,我的资产文件夹中有一个SQLite数据库的副本。据我所知,它运转良好。当我的应用程序第一次在emulator中安装时,会出现如下错误:

下面是我尝试过的另一种方法:

checkDB = SQLiteDatabase.openDatabase(mPath, null,
                SQLiteDatabase.OPEN_READONLY);
我也试着用这个:

 SQLiteDatabase.openDatabase(mPath, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);

关于如何解决这个问题有什么建议吗?

我总是按照以下步骤从资产文件夹复制数据库:

  private void copyDatabase() throws IOException{

        InputStream inputStream = context.getAssets().open(DB_NAME);
        String dbCreatePath = DB_PATH+DB_NAME;
        OutputStream outputStream = new FileOutputStream(dbCreatePath);
        byte[] buffer = new byte[1024];
        int length;
        while((length = inputStream.read(buffer))>0){
            outputStream.write(buffer,0,length);
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();
}
一旦它被复制,我将进行如下检查:

  private boolean checkDatabase(){
    SQLiteDatabase checkDB = null;
    try {
        String dbPath = DB_PATH+DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(dbPath, null,     
            SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
        // TODO: handle exception
    }
    if(checkDB!=null){
        checkDB.close();}
    return checkDB != null ? true:false;

}

我遇到了一个类似的问题,不同之处在于数据库在我的EclipseNexus7模拟器中工作,而不是在Nexus7本身上工作。我下载了Questoid,以便在从“资产”文件夹复制数据库后能够查看数据库中的内容,但有一段时间,数据库甚至没有显示在“文件资源管理器”中。我一直在复印,最后它终于出现了。在经历了多次相同的过程后,我还没有弄清楚什么是共同点,但重复似乎是我的关键。现在我仍然需要弄清楚为什么我的Nexus7在安装应用程序时无法打开数据库。我也遇到了同样的例外,但仅限于设备本身。

看到这篇文章可能很有帮助。我已经尝试过这篇文章了。Hi Chiru,你复制过你的数据库吗?我们可以看到您从PATH复制db的代码部分吗?我在添加代码SqliteContoPendDatabaseException后遇到此异常:无法打开数据库文件。请在我添加日志后进行检查。您是否尝试在openDatabase()之前调用createDatabase()。如下所示:URLDatabaseHelper dbHelper=newurldatabasehelper(getApplicationContext());dbHelper.createDatabase();dbHelper.openDatabase();我在checkDB=SQLiteDatabase.openDatabase(dbPath,null,SQLiteDatabase.OPEN\u READWRITE)上出错;是的,我在checkDB=SQLiteDatabase.openDatabase(dbPath,null,SQLiteDatabase.OPEN_READWRITE)上得到了相同的错误;我的数据库路径是“/data/data//databases/db.s3db”
  private void copyDatabase() throws IOException{

        InputStream inputStream = context.getAssets().open(DB_NAME);
        String dbCreatePath = DB_PATH+DB_NAME;
        OutputStream outputStream = new FileOutputStream(dbCreatePath);
        byte[] buffer = new byte[1024];
        int length;
        while((length = inputStream.read(buffer))>0){
            outputStream.write(buffer,0,length);
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();
}
  private boolean checkDatabase(){
    SQLiteDatabase checkDB = null;
    try {
        String dbPath = DB_PATH+DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(dbPath, null,     
            SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
        // TODO: handle exception
    }
    if(checkDB!=null){
        checkDB.close();}
    return checkDB != null ? true:false;

}