我将我的数据库存储在我的资源文件夹中,并在运行时复制.我目前有一个简单的Activity,它可以进行简单的数据库调用:
DBAdapter adapter = new DBAdapter(HomeActivity.this);
final SQLiteDatabase db = adapter.getReadableDatabase();
final Cursor c = db.query("exercises", new String[] { "name" }, null,
null, null, null, null);
startManagingCursor(c);
if (c.moveToFirst())
Log.e(TAG, c.getString(0));
else
Log.e(TAG,"No dice");
下面是我的DBAdapter.java(扩展了open helper):
public class DBAdapter extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static String DB_PATH = "";
private static final String DB_NAME = "gymrat.db";
private final Context myContext;
private static final String TAG = "GymRat.DBAdapter";
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DBAdapter(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
DB_PATH = "/data/data/"
+ context.getApplicationContext().getPackageName()
+ "/databases/";
}
/**
* Copies your database from your local assets-folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transferring bytestream.
* */
private void copyDatabase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
/**
* Call on creating data base for example for creating tables at run time
*/
@Override
public void onCreate(SQLiteDatabase db) {
Log.e(TAG, "onCreate");
try {
copyDatabase();
} catch (IOException e) {
Log.e(TAG,"IOException copying Database");
}
}
@Override
public void onOpen(SQLiteDatabase db) {
Log.e(TAG, "onOpen");
try {
db.rawQuery("select * from exercises", null);
} catch ( SQLiteException e) {
Log.e(TAG,"DB copy didn't work");
try {
copyDatabase();
} catch (IOException e1) {
Log.e(TAG,"IOException recopying DB");
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG, "Upgrading");
}
}
我从here开始使用此适配器并修改它,因为它似乎没有使用OpenHelper功能.
我的问题:每当我的应用程序在安装后第一次运行,或者在应用程序上擦除数据之后,onCreate将首先按照我的预期进行调用,并调用copyDatabase方法.问题是数据库没有被复制 – 或者如果它被复制,它会被立即覆盖.而是创建仅具有“android meta_data”表的默认数据库,导致我的活动中的查询抛出异常,因为该表不存在.
如果我在onOpen方法中再次复制数据库,它可以正常工作.我是在做一些乱序或缺少一些导致SQLiteOpenHelper创建默认数据库的调用吗?创建的默认数据库使用构造函数中指定的相同名称和版本号.
现在,正如您所看到的,我正在使用onOpen中的虚拟查询来查看预期的表是否存在以及是否未继续并重新复制数据库.另一种选择是在onCreate被称为信号onOpen时设置一个标志来复制数据库.显然这些都有点hacky,我真的很好奇发生了什么.
我计划将实际的数据库调用移动到Activity之外的单独的辅助类,我只是直接调用db来测试它.
解决方法:
如果要在第一次运行时从资产复制数据库并从data / data / program.name / database打开数据库,则应使用如下代码:
public class YourDatabase extends SQLiteOpenHelper
public YourDatabase(Context context) {
super(context, DatabaseName, null, DATABASE_VERSION);
this.dbContext = context;
// checking database and open it if exists
if (checkDataBase()) {
openDataBase();
} else {
try {
this.getReadableDatabase();
copyDataBase();
this.close();
openDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
Toast.makeText(context, "Initial database is created", Toast.LENGTH_LONG).show();
}
}
完整的答案你可以阅读this问题
原文地址:https://codeday.me/bug/20190621/1255513.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。