Can';t从资产文件夹[Android]打开数据库
很抱歉,这个问题很难解决,但我要4个小时才能解决。我正在尝试从/assets文件夹复制数据库,该文件夹可与SQLiteOpenHelper一起使用,但当我尝试打开InputStream时,会出现错误:Can';t从资产文件夹[Android]打开数据库,android,sqlite,Android,Sqlite,很抱歉,这个问题很难解决,但我要4个小时才能解决。我正在尝试从/assets文件夹复制数据库,该文件夹可与SQLiteOpenHelper一起使用,但当我尝试打开InputStream时,会出现错误: E/Trace: error opening trace file: No such file or directory (2) 代码: 你能试试这个代码,然后告诉我结果吗。 确保检查了所有文件名 @Override protected void onCreate(Bundle savedIn
E/Trace: error opening trace file: No such file or directory (2)
代码:
你能试试这个代码,然后告诉我结果吗。 确保检查了所有文件名
@Override
protected void onCreate(Bundle savedInstanceState) {
//.......
DatabaseHelper dbHelper = new DatabaseHelper(this);
dbHelper.createDatabase();
dbHelper.openDatabase();
// do stuff
Cursor data =dbHelper.Sample_use_of_helper();
dbHelper.close();
}
class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.mycomp.myapp/databases/";
private static String DB_NAME = "database.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DatabaseHelper (Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void crateDatabase() throws IOException {
boolean vtVarMi = isDatabaseExist();
if (!vtVarMi) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
private boolean isDatabaseExist() {
SQLiteDatabase kontrol = null;
try {
String myPath = DB_PATH + DB_NAME;
kontrol = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
kontrol = null;
}
if (kontrol != null) {
kontrol.close();
}
return kontrol != null ? true : false;
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public Cursor Sample_use_of_helper() {
return myDataBase.query("TABLE_NAME", null, null, null, null, null, null);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
从db_NAME和.open(“database.db”)中删除.db扩展名
它看起来很难看,但现在同样的东西对我起作用了……
在安装新的apk之前,请清除早期安装的apk的数据
如果它仍然不起作用,请说明您的数据库大小。我真的建议您使用
SQLiteAssetHelper
而不是滚动您自己的代码:您可以发布“copydatabase”功能的其余部分吗?当然可以。我刚刚编辑了我的帖子。您在哪里尝试从Assets文件夹打开文件?看起来更像是试图从外部存储器上的文件夹打开sqlite db。我用sqlite浏览器创建了一个.db文件,然后导入到AndroidStudio中。程序会自动创建带有该文件的资产文件夹,但在运行程序后,会发生错误。我真的不知道怎么了,谢谢!你的代码工作没有任何问题。我不确定我错过了什么,但我会努力找出答案并与大家分享。@AdamK我只是在猜测。也许你的代码只在第一次工作。之后,系统无法覆盖数据库文件。最终解决了这个问题。问题不在于copyDatabase方法,而在于我对DatabaseHelper类角色和getReadableDatabase()方法的误解。谢谢你的帮助!
@Override
protected void onCreate(Bundle savedInstanceState) {
//.......
DatabaseHelper dbHelper = new DatabaseHelper(this);
dbHelper.createDatabase();
dbHelper.openDatabase();
// do stuff
Cursor data =dbHelper.Sample_use_of_helper();
dbHelper.close();
}
class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.mycomp.myapp/databases/";
private static String DB_NAME = "database.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DatabaseHelper (Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void crateDatabase() throws IOException {
boolean vtVarMi = isDatabaseExist();
if (!vtVarMi) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
private boolean isDatabaseExist() {
SQLiteDatabase kontrol = null;
try {
String myPath = DB_PATH + DB_NAME;
kontrol = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
kontrol = null;
}
if (kontrol != null) {
kontrol.close();
}
return kontrol != null ? true : false;
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public Cursor Sample_use_of_helper() {
return myDataBase.query("TABLE_NAME", null, null, null, null, null, null);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}