1.Android 数据库 放在assets文件夹下,但是读取数据库时注意数据的地址path是: data/data/包名/files/数据库名;
eg: data/data/com.zh.mobilesafe/files/address.db
获得数据库的对象;
private static final String PATH = “data/data/com.zh.mobilesafe/files/address.db”;
SQLiteDatabase db = SQLiteDatabase.openDatabase(PATH, null, SQLiteDatabase.OPEN_READONLY);
2、数据库的加载初始化一般是在splash 闪屏页面进行处理的。
拷贝数据库,动态的。
private void copyDb(String dbName) {
File file = new File(getFilesDir(), dbName);// 文件存放的地址。 文件夹名+文件名字。
FileOutputStream out = null;
InputStream is = null;
try {
// 得到一个输入 流。
is = getAssets().open(dbName); //动态的获取数据库名。
out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
is.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}