Tôi có tệp .db trong thư mục assets
của mình. Tôi đã sao chép nó vào thư mục data/data/<packagename>/databases/
trong trình mô phỏng và hoạt động của nó ..Cơ sở dữ liệu không được sao chép từ thư mục tài sản sang thiết bị
Nhưng khi tôi chạy nó trên thiết bị, nó buộc phải đóng. Nó được thể hiện
SQLite exception: no such table: tbl_user
Đây là mã của tôi ..
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper() {
super(dataContext, DATABASE_NAME, null, 1);
DB_PATH = "/data/data/"
+ dataContext.getApplicationContext().getPackageName()
+ "/databases/";
Log.d("PATH", DB_PATH);
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.d("Error", e.toString());
}
}
}
private void copyDataBase() throws IOException {
// TODO Auto-generated method stub
InputStream inFile = dataContext.getAssets().open(DATABASE_NAME);
String outFileName = DB_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = inFile.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
inFile.close();
}
private boolean checkDataBase() {
// TODO Auto-generated method stub
File dbFile = new File(DB_PATH + DATABASE_NAME);
return dbFile.exists();
}
tôi nên phải làm cái gì khác để sao chép rằng db đến thiết bị ???
Cảm ơn ..
Tôi thực sự khuyên bạn nên sử dụng 'SQLiteAssetHelper'. thay vì lăn của riêng bạn: https://github.com/jgilfelt/android-sqlite-asset-helper – CommonsWare
vui lòng kiểm tra liên kết này http://www.reigndesign.com/blog/using-your-own-sqlite-database- trong ứng dụng android / –