1,首先把数据库拷贝到如图文件夹
2,要把数据库加载到data/data/包名/files/数据库名.db
3,代码
// 复制数据库 data/data/com.songzhen.mobilesafe/files/address.db
try {// 加载数据库
InputStream is = getAssets().open("address.db");
File file = new File(getFilesDir(), "address.db");
if (file.exists() && file.length() > 0) {
Toast.makeText(this, "数据库之前加载完毕", 0).show();
} else {
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
Toast.makeText(this, "数据库第一次加载完毕", 0).show();
}
} catch (IOException e) {
e.printStackTrace();
}
4,用数据库
String path = "data/data/com.songzhen.mobilesafe/files/address.db";
SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
Cursor cursor = sqLiteDatabase.rawQuery(
"select location from data2 where id=(select outkey from data1 where id=?)", new String[] { num.substring(0,7) });
while (cursor.moveToNext()) {
area = cursor.getString(0);
}
cursor.close();