gpt4 book ai didi

java - 打开 Sqlite DB Android 时出现 FileNotFound 异常

转载 作者:太空宇宙 更新时间:2023-11-04 10:07:05 27 4
gpt4 key购买 nike

我正在尝试在我的 Android 项目上打开现有的 sqlite 数据库,但第二次运行时出现 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数据库,当我运行代码时,我在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 异常。我正在尝试打开 Assets 内数据库文件夹中的现有数据库,但无法打开该数据库,可能的错误是什么?我无法找出确切的错误,如果有人可以帮助我编写可从 Assets 文件夹打开现有数据库的代码,谢谢。

最佳答案

我相信找不到文件异常的原因是因为您正在使用:-

myInput = context.getAssets().open(DB_LOCATION + DB_NAME);

这将尝试在 Assets 文件夹中查找名为 /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);

因为,除非该文件存在,否则会发生第二个文件未找到异常。

此外,您使用的路径不正确,而不是 /data/user/0/amalaugustine.example.com.dusyllabus/files/databases/dusyllabus

路径实际上应该是/data/user/0/amalaugustine.example.com.dusyllabus/databases/dusyllabus

此外,您尝试使用的代码将导致每次创建 SyllabusHelper 类的实例时从 Assets 文件中复制数据库。因为没有检查数据库当前是否存在。

建议完整修复:-

我建议为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

关于java - 打开 Sqlite DB Android 时出现 FileNotFound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52785468/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com