[新手]我使用原始数据库文件(.sqlite格式)作为资产文件包含3个表(android_metadata,categories,crop)并使用我的手机作为调试设备。完成调试后,我通过Android设备监视器检查手机中的数据库,使用“从设备中提取文件”功能来读取我的数据库。 android_metadata存在,但不存在另外两个。这是我的完整代码,请在需要的地方更正。
DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH= "data/data/com.mundane.hortipedia/assets/databases/";
private static String DB_NAME = "horticulture.sqlite";
private SQLiteDatabase myDB;
private final Context context;
public DatabaseHelper(Context context) throws IOException {
super(context, DB_NAME , null, 3);
this.context = context;
boolean dbexist = checkDB();
if(dbexist){
openDB();
}else{
System.out.println("Database doesn't exist");
createDB();
}
}
public void createDB() throws IOException {
this.getReadableDatabase();
Log.i("Readable ends...", "end");
try {
copyDB();
Log.i("Copy db ends...","end");
} catch (IOException e) {
throw new Error("Error copying database");
}
}
private boolean checkDB(){
boolean checkDB = false;
try{
String myPath = DB_PATH + DB_NAME;
File dbFile = new File(myPath);
SQLiteDatabase.OPEN_READONLY);
checkDB = dbFile.exists();
} catch(SQLiteException e){
System.out.println("Database doesn't exist");
}
return checkDB;
}
public void copyDB() throws IOException{
try {
Log.i("inside copyDB...","start");
InputStream myInput = context.getAssets().open(DB_NAME);
Log.i("Input Stream....",myInput+"");
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);
Log.i("Content.... ",length+"");
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e) {
Log.v("error", e.toString());
}
}
public void openDB() throws SQLiteException {
String myPath = DB_PATH + DB_NAME;
myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
Log.i("open DB......",myDB.toString());
}
@Override
public synchronized void close() {
if(myDB != null)
myDB.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
}
}
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.fragment_drawer);
// Set up drawer
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
try {
myDbHelper = new DatabaseHelper(this);
myDbHelper.createDB();
} catch (IOException e) {
throw new Error("Unable to create database");
}
myDbHelper.openDB();
}
答案 0 :(得分:0)
更新代码:
1.在 build.gradle
的依赖项中添加此行dependencies {
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
}
2.确保您已将原始.sqlite或.db文件放在目录 ...(您的应用)/ src / main / assets / databases / 中,然后将您的助手扩展到SQLiteAssetHelper
DBHelper.java
public class DBHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "horticulture.sqlite";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase db;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//some method to access your db
}
我的DBHelper用法示例: MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_
DBHelper dbHelper = new DBHelper(getApplicationContext());
}