我们已经为 Android 平台开发了一个预加载了 SQLite 数据库的应用程序。使用 SQLiteOpenHelper,在第一次执行应用程序时,我们从“Assets”复制数据库到应用程序数据目录(/data/data/br.com.lwu/databases/appdatabase.sqlite)。 这适用于大多数设备。但我们在使用 LG 手机时遇到以下问题:
- LG Optimus One
- 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;
}
}
最佳答案
尝试改变:
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
对此:
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
这样做不需要您拥有 android vm 所需的强制表 android_metadata
,以防它在您的数据库中丢失。
关于android - LG手机无法访问SqLite数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11049889/