我们已经为 Android 平台开发了一个预加载 SQLite 数据库的应用程序。使用 SQLiteOpenHelper,在应用程序的第一次执行中,我们从“Assets”中复制数据库为应用程序数据目录(/data/data/br.com.lwu/databases/appdatabase.sqlite)。这适用于大多数设备。但是我们在使用以下 LG 手机时遇到了问题:
- LG 擎天柱一号
- LG-P500
- LG-P500h
显然,数据库已正确复制。但应用程序无法访问数据库(如表)。我们需要帮助!
// Copy Database:
private static String DB_PATH = "/data/data/br.com.lwu/databases/";
private static String DB_NAME = "appdatabase.sqlite";
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
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);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
if (myDataBase == null) {
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
}
@Override
public synchronized void close() {
super.close();
if (myDataBase != null)
if (myDataBase.isOpen()) {
myDataBase.close();
myDataBase = null;
}
}