java - 打开 Sqlite DB Android 时出现 FileNotFound 异常
问题描述
我正在尝试在我的 android 项目上打开一个现有的 sqlite db,它在第二次运行时出现 Filenotfound 异常,在第一次运行时出现另一个错误。
public class SyllabusHelper extends SQLiteOpenHelper {
private static Context mContext;
public static final String DB_NAME="dusyllabus";
public static String DB_LOCATION= "/data/data/com.example.amalaugustine.dusyllabus/databases/";
private static int DB_VERSION=1;
private SQLiteDatabase mDB;
public SyllabusHelper(Context context) {
super(context,DB_NAME,null,1);
ContextWrapper cw =new ContextWrapper(context);
DB_LOCATION=cw.getFilesDir().getAbsolutePath()+ "/databases/";
Log.v("DBLOCATIONA",DB_LOCATION);
this.mContext = context;
copyDatabase(mContext);
}
public void OpenDatabase(){
String dbPath=mContext.getDatabasePath(DB_NAME).getPath();
Log.v("dbpatH",dbPath.toString());
if(mDB!=null && mDB.isOpen()){
return;
}
mDB=SQLiteDatabase.openDatabase(DB_LOCATION,null,SQLiteDatabase.OPEN_READWRITE);
}
private boolean copyDatabase(Context context) {
Log.i("Database",
"New database is being copied to device!");
byte[] buffer = new byte[1024];
OutputStream myOutput = null;
int length;
// Open your local db as the input stream
InputStream myInput = null;
try {
myInput = context.getAssets().open(DB_LOCATION + DB_NAME);
// transfer bytes from the inputfile to the
// outputfile
myOutput = new FileOutputStream(DB_LOCATION + DB_NAME);
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.close();
myOutput.flush();
myInput.close();
Log.i("Database",
"New database has been copied to device!");
return true;
} catch (Exception e) {
e.printStackTrace();
Log.v("QuizActivity", e.toString());
return false;
}
}
上面的代码是在android上打开一个现有的sqlite db,当我运行代码时,我在logcat上得到一个错误
10-12 13:50:10.389 9813-9813/com.example.amalaugustine.dusyllabus E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
PATH2 /vendor/lib64/hw/gralloc.qcom.so
PATH1 /system/lib64/hw/gralloc.qcom.so
PATH3 /odm/lib64/hw/gralloc.msm8953.so
PATH2 /vendor/lib64/hw/gralloc.msm8953.so
PATH1 /system/lib64/hw/gralloc.msm8953.so
当我在运行时打开应用程序时,它崩溃了,logcat 上的错误是 filenotfound 异常。我正在尝试打开资产内我的数据库文件夹中的现有数据库,但我无法打开此数据库,可能的错误是什么?我无法找出确切的错误,谢谢,如果有人可以帮助我使用可从资产文件夹打开现有数据库的代码。
解决方案
我相信File Not Found异常的原因是因为您使用的是:-
myInput = context.getAssets().open(DB_LOCATION + DB_NAME);
这将尝试查找/data/user/0/amalaugustine.example.com.dusyllabus/files/databases/dusyllabus
资产文件夹中命名的文件。
您可能应该只寻找数据库文件,所以
myInput = context.getAssets().open(DB_NAME);
这是假设名为dusyllabus的文件不在 assets 文件夹的子目录中,而是直接在 assets 文件夹中。
但是,你会有一个问题
myOutput = new FileOutputStream(DB_LOCATION + DB_NAME);
因为,除非该文件存在,否则将发生 2nd File not found 异常。
此外,您使用的路径不正确,而不是/data/user/0/amalaugustine.example.com.dusyllabus/files/databases/dusyllabus
路径实际上应该是/data/user/0/amalaugustine.example.com.dusyllabus/databases/dusyllabus
此外,您尝试使用的代码将导致每次创建 SyllabusHelper 类的实例时从资产文件中复制数据库。因为没有检查数据库当前是否存在。
建议的完整修复:-
我建议SyllabusHelper.java使用以下代码:-
public class SyllabusHelper extends SQLiteOpenHelper {
private static Context mContext;
public static final String DB_NAME = "dusyllabus";
//public static String DB_LOCATION = "/data/data/com.example.amalaugustine.dusyllabus/databases/"; //<<<<<<<<<< not needed
public String db_location; //<<<<<<<<<< ADDED
private static int DB_VERSION = 1;
private SQLiteDatabase mDB;
public SyllabusHelper(Context context) {
super(context, DB_NAME, null, 1);
//ContextWrapper cw = new ContextWrapper(context); //<<<<<<<<<< NOT NEEDED
//DB_LOCATION = cw.getFilesDir().getAbsolutePath() + "/databases/"; //<<<<<<<<<<WRONG
db_location = context.getDatabasePath(DB_NAME).getPath(); //<<<<<<<<<< recommended way to get the path
//Log.v("DBLOCATIONA", DB_LOCATION); //<<<<<<<<<< not used
Log.v("DBLOCATIONB",db_location); //<<<<<<<<<< changed
this.mContext = context;
copyDatabase(mContext);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
Log.v("ONCREATE","On Create has been called");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public void OpenDatabase() {
String dbPath = mContext.getDatabasePath(DB_NAME).getPath();
Log.v("dbpatH", dbPath.toString());
if (mDB != null && mDB.isOpen()) {
return;
}
mDB = this.getWritableDatabase();
}
private boolean copyDatabase(Context context) {
if (doesDBExist()) return true; //<<<<<<<<<< Check if database already exists
Log.i("Database",
"New database is being copied to device!");
byte[] buffer = new byte[1024];
OutputStream myOutput = null;
int length;
// Open your local db as the input stream
InputStream myInput = null;
try {
myInput = context.getAssets().open(DB_NAME); //<<<<<<<<<< only get according to the file name not the path
// transfer bytes from the inputfile to the
// outputfile
File newdb = new File(db_location); //<<<<<<<<<< get the file so that it exists
myOutput = new FileOutputStream(newdb); //<<<<<<<<<< uses the file
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.close();
myOutput.flush();
myInput.close();
Log.i("Database",
"New database has been copied to device!");
return true;
} catch (Exception e) {
e.printStackTrace();
Log.v("QuizActivity", e.toString());
return false;
}
}
//<<<<<<<<<<< ADDED to check for existence of db file
//!!NOTE!! does not check that the file is a valid database
private boolean doesDBExist() {
File db = new File(db_location);
if (db.exists()) {
return true;
}
File dbdir = new File(db.getParent());
dbdir.mkdirs();
return false;
}
}
当然你总是可以使用SQLiteAssetHelper
推荐阅读
- python - 在 cvxpy 中指定约束时违反了 DCP 要求,可能需要重新考虑问题的整个表述
- android - Kotlin:为什么转换为字符串后 ByteArray 的大小不同?
- if-statement - 纯 Prolog Peano List 交集
- .net-core - 在代理上使用什么 .Net Core 版本
- reactjs - Redux-Saga 不使用 redux-saga-firebase 等待操作完成
- reactjs - 在自定义输入字段 React-Final-Form-Arrays 上键入时失去焦点
- bash - 为什么我的递归 for 循环不断重复列表中的第一项?
- scala - 读取最后一个增量分区而不读取所有增量
- python - Python嵌套字典不断写所有的kay值
- javascript - 有没有办法从执行函数的范围中获取变量?