开发者社区> 问答> 正文

SQLite不能打开数据库的异常

a123456678 2016-07-18 14:01:30 818

在程序的中我从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);
可以帮我看下问题所在吗?谢谢!

数据库 数据库管理
分享到
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:57:48

    我经常用下面的步骤从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;
    
    }
    0 0
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

推荐文章
相似问题
推荐课程