Android 首次升级sqlite数据库时应用程序崩溃
第一次更新sqlite数据库时,我的应用程序发生崩溃。重新加载应用程序,从那时起就可以正常工作。我猜这与onUpgrade函数有关。我似乎找不到问题所在,任何建议都非常感谢。提前谢谢 数据库助手:Android 首次升级sqlite数据库时应用程序崩溃,android,sqlite,crash,upgrade,Android,Sqlite,Crash,Upgrade,第一次更新sqlite数据库时,我的应用程序发生崩溃。重新加载应用程序,从那时起就可以正常工作。我猜这与onUpgrade函数有关。我似乎找不到问题所在,任何建议都非常感谢。提前谢谢 数据库助手: public class DatabaseHelper extends SQLiteOpenHelper { private static String DB_PATH = "/data/data/jp.atomicideas.ne/databases/"; private stat
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/jp.atomicideas.ne/databases/";
private static String DB_NAME = "dataset";
public static final int DB_VERSION = 2;
private SQLiteDatabase myDataBase;
private final Context myContext;
public static final String EXPRESSION_TABLE = "expression";
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access
* the assets and resources.
*
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
/**
* Creates an empty database on the system and rewrites with database from app
* @throws IOException
*/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
// if the database already exists, do nothing
if(dbExist){
Log.v("DB Exists", "db exists");
// by calling this method here, onUpgrade will be called on a writeable db, if version number is bumped
this.getWritableDatabase();
}
dbExist = checkDataBase();
// if the database doesn't exist, copy the application's database to be used
if(!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exists to avoid re-copying the file
* @return true only if it exists, falls if it doesnt
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String mypath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READONLY);
} catch(SQLiteException e) {
// database does not exist yet
}
if(checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies database from the local assets folder to the system folder
* @throws IOException
*/
private void copyDataBase() throws IOException {
// Open the app database file as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the empty temporary database to be replaced
String outFileName = DB_PATH + DB_NAME;
// Open the empty database file as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Toast.makeText(myContext, "onUpgrade called!", Toast.LENGTH_LONG).show();
if (oldVersion < newVersion) {
Log.v("Database Upgrade", "Database version higher, upgrading");
myContext.deleteDatabase(DB_NAME);
}
}
}
及
您不需要删除数据库,只需使用您最近定义的方法(
copyDataBase
)复制数据库即可,如下所示:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Toast.makeText(myContext, "onUpgrade called!", Toast.LENGTH_LONG).show();
if (oldVersion < newVersion) {
Log.v("Database Upgrade", "Database version higher, upgrading");
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error upgrading database");
}
}
}
@覆盖
public void onUpgrade(SQLiteDatabase db,intoldversion,intnewversion){
Toast.makeText(myContext,“onUpgrade called!”,Toast.LENGTH\u LONG.show();
如果(旧版本<新版本){
Log.v(“数据库升级”,“数据库版本更高,升级”);
试试{
copyDataBase();
}捕获(IOE){
抛出新错误(“升级数据库时出错”);
}
}
}
您好,谢谢您的回复。这会导致应用程序每次崩溃,并在LogCat:06-29 21:18:46.407:E/AndroidRuntime(2028)中显示此消息:原因:java.lang.IllegalStateException:getReadableDatabase调用recursivelyAh,然后它已经打开,您可以删除该行。没问题,我将编辑掉这一行,这样未来的访问者就不必阅读评论就能得到有效的答案。
06-28 20:52:07.678: E/AndroidRuntime(26580): Caused by: android.database.sqlite.SQLiteDiskIOException: disk I/O error: PRAGMA user_version = 2
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Toast.makeText(myContext, "onUpgrade called!", Toast.LENGTH_LONG).show();
if (oldVersion < newVersion) {
Log.v("Database Upgrade", "Database version higher, upgrading");
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error upgrading database");
}
}
}