我正在尝试在我的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文件夹中打开现有数据库的代码来帮助我,则谢谢。
答案 0 :(得分:0)
我相信未找到文件的原因是因为您使用的是:-
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 的文件不在资产文件夹的子目录中,而是直接在资产文件夹中。
但是,您将会遇到一个问题
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类的实例时,尝试使用的代码都会导致从资产文件复制数据库。由于没有检查数据库当前是否存在。
我建议为 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