如何在资产文件夹中加载大于1 MB的数据库?我有一个数据库文件,我将其复制到资产文件夹然后通过代码复制到数据目录,但因为它大于1 MB,所以会出错!
答案 0 :(得分:0)
SQLiteAssetHelper库使这项任务非常简单 jar文件和文档位于https://github.com/jgilfelt/android-sqlite-asset-helper。
如文档中所述:
android-sqlite-asset-helper.jar
添加到项目libs
目录。assets/databases/database_name.zip
。示例:
public class MyDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "database_name";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
修改
public Cursor getEmployees() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String [] sqlSelect = {"0 _id", "FullName"};
String sqlTables = "Employees";
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, null, null,
null, null, null);
c.moveToFirst();
return c;
}
public int getUpgradeVersion() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String [] sqlSelect = {"MAX (version)"};
String sqlTables = "upgrades";
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, null, null,
null, null, null);
int v = 0;
c.moveToFirst();
if (!c.isAfterLast()) {
v = c.getInt(0);
}
c.close();
return v;
}