我正在使用GreenDao
ORM库来创建数据库,它可以很好地运行第一个创建数据库的示例。
但是现在的问题是,我找不到任何文档或与从资产文件夹复制数据库到内存中的数据库有关的内容。
已接受的答案提供了一个解决方案,但它只是部分正确的。请查看我在下面发布的类。这是greenDao OpenHelper类的扩展。在此之下是一个使用示例,以及您如何使用它来代替OpenHelper或DevOpenHelper来管理您的sqlite数据库副本。
package com.example;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import com.example.R;
import com.example.DaoMaster.OpenHelper;
public class DatabaseOpenHelper extends OpenHelper {
private Context context;
private SQLiteDatabase sqliteDatabase;
private static String DB_PATH;
private static String DB_NAME;
public DatabaseOpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory);
this.context = context;
this.DB_NAME = name;
this.DB_PATH = context.getString(R.string.DB_PATH);
try {
createDataBase();
} catch (Exception ioe) {
throw new Error("Unable to create database");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
/** Open Database for Use */
public void openDatabase() {
String databasePath = DB_PATH + DB_NAME;
sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
(SQLiteDatabase.OPEN_READWRITE));
}
/** Close Database after use */
@Override
public synchronized void close() {
if ((sqliteDatabase != null) && sqliteDatabase.isOpen()) {
sqliteDatabase.close();
}
super.close();
}
/** Get database instance for use */
public SQLiteDatabase getSqliteDatabase() {
return sqliteDatabase;
}
/** Create new database if not present */
public void createDataBase() {
SQLiteDatabase sqliteDatabase = null;
if (databaseExists()) {
/* Check for Upgrade */
} else {
/* Database does not exists create blank database */
sqliteDatabase = this.getReadableDatabase();
sqliteDatabase.close();
copyDataBase();
}
}
/** Check Database if it exists */
private boolean databaseExists() {
SQLiteDatabase sqliteDatabase = null;
try {
String databasePath = DB_PATH + DB_NAME;
sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (sqliteDatabase != null) {
sqliteDatabase.close();
}
return sqliteDatabase != null ? true : false;
}
/**
* Copy existing database file in system
*/
public void copyDataBase() {
int length;
byte[] buffer = new byte[1024];
String databasePath = DB_PATH + DB_NAME;
try {
InputStream databaseInputFile = this.context.getAssets().open(DB_NAME+".sqlite");
OutputStream databaseOutputFile = new FileOutputStream(databasePath);
while ((length = databaseInputFile.read(buffer)) > 0) {
databaseOutputFile.write(buffer, 0, length);
databaseOutputFile.flush();
}
databaseInputFile.close();
databaseOutputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这里是它使用的一个例子:
DatabaseOpenHelper helper = new DatabaseOpenHelper(this, "db", null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
SqliteOpenHelper
类创建数据库的基本方法,只是我需要使用这个名为GreenDao
的ORM
工具,并了解它如何使用其库完成相同的操作。 - Some one Some where