这是我在DatabaseHelper.class
public class DataBaseHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;
private static final String DATABASE_NAME = "database_EN_db";
private static File DATABASE_FILE;
// This is an indicator if we need to copy the
// database file.
private boolean mInvalidDatabaseFile = false;
private boolean mIsUpgraded = false;
private Context mContext;
/**
* number of users of the database connection.
* */
private int mOpenConnections = 0;
private static DataBaseHelper mInstance;
synchronized static public DataBaseHelper getInstance(Context context) {
if (mInstance == null) {
mInstance = new DataBaseHelper(context.getApplicationContext());
}
return mInstance;
}
private DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
this.mContext = context;
SQLiteDatabase db = null;
try {
db = getReadableDatabase();
if (db != null) {
db.close();
}
DATABASE_FILE = context.getDatabasePath(DATABASE_NAME);
if (mInvalidDatabaseFile) {
copyDatabase();
}
if (mIsUpgraded) {
doUpgrade();
}
} catch (SQLiteException e) {
} finally {
if (db != null && db.isOpen()) {
db.close();
}
}
}
@Override
public void onCreate(SQLiteDatabase db) {
mInvalidDatabaseFile = true;
}
@Override
public void onUpgrade(SQLiteDatabase database,
int old_version, int new_version) {
mInvalidDatabaseFile = true;
mIsUpgraded = true;
}
/**
* called if a database upgrade is needed
*/
private void doUpgrade() {
// implement the database upgrade here.
}
@Override
public synchronized void onOpen(SQLiteDatabase db) {
super.onOpen(db);
// increment the number of users of the database connection.
mOpenConnections++;
if (!db.isReadOnly()) {
// Enable foreign key constraints
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
/**
* implementation to avoid closing the database connection while it is in
* use by others.
*/
@Override
public synchronized void close() {
mOpenConnections--;
if (mOpenConnections == 0) {
super.close();
}
}
private void copyDatabase() {
AssetManager assetManager = mContext.getResources().getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(DATABASE_NAME);
out = new FileOutputStream(DATABASE_FILE);
byte[] buffer = new byte[1024];
int read = 0;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
} catch (IOException e) {
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {}
}
}
setDatabaseVersion();
mInvalidDatabaseFile = false;
}
private void setDatabaseVersion() {
SQLiteDatabase db = null;
try {
db = SQLiteDatabase.openDatabase(DATABASE_FILE.getAbsolutePath(), null,
SQLiteDatabase.OPEN_READWRITE);
db.execSQL("PRAGMA user_version = " + VERSION);
} catch (SQLiteException e ) {
} finally {
if (db != null && db.isOpen()) {
db.close();
}
}
}
}
这是我的数据库DAO:
public class DBDAO {
protected SQLiteDatabase database;
private DataBaseHelper dbHelper;
private Context mContext;
public DBDAO(Context context) {
this.mContext = context;
dbHelper = DataBaseHelper.getInstance(mContext);
open();
}
public DBDAO() {
// TODO Auto-generated constructor stub
}
public void open() throws SQLException {
if(MainActivity.lang == "English"){
if(dbHelper == null)
dbHelper = DataBaseHelper.getInstance(mContext);
database = dbHelper.getWritableDatabase();
}
}
}
我创建了一个DAO扩展DBDAO并使用此DAO访问数据库:
public class CauHoiDAO extends DBDAO{
public CauHoiDAO(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}
我需要什么..
但是,结果是错误,因为没有数据库而无法找到表格; 那么,为什么我无法从资源文件夹中预加载我的数据库? 有人可以帮我吗??????