开发者社区> 问答> 正文

SQLite不能打开数据库的异常

在程序的中我从assets中复制了SQLite数据库,可以使用。当把程序第一次安装在仿真器中运行时,获得以下错误。
Failed to open the database. closing it.

android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file

E/SQLiteDatabase(7516): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
E/SQLiteDatabase(29308): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1013)

E/SQLiteDatabase(29308): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)

E/SQLiteDatabase(29308): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)

E/SQLiteDatabase(29308): at com.guayama.database.URLDatabaseHelper.checkDBExists(URLDatabaseHelper.java:86)

E/SQLiteDatabase(29308): at com.guayama.database.URLDatabaseHelper.createURLDB(URLDatabaseHelper.java:54)

E/SQLiteDatabase(29308): at com.guayama.database.URLDatabaseHelper.(URLDatabaseHelper.java:38)
打开数据库用的代码:
SQLiteDatabase.openDatabase(mPath, null,SQLiteDatabase.CREATE_IF_NECESSARY);
也试过以下的代码打开:
checkDB = SQLiteDatabase.openDatabase(mPath, null,

            SQLiteDatabase.OPEN_READONLY);

还有这个代码片段:
SQLiteDatabase.openDatabase(mPath, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);
可以帮我看下问题所在吗?谢谢!

展开
收起
a123456678 2016-07-18 14:01:30 2572 0
1 条回答
写回答
取消 提交回答
  • 我经常用下面的步骤从assets文件夹中复制数据库

    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;
    
    }
    2019-07-17 19:57:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载