[英]Sqlite database not updating with app update
我正在创建一个使用Sqlite数据库的应用程序。 该数据库包含一个表,我正在使用sqlitebrowser更新该表。 但是,每当我启动数据库时,更改都不会在应用程序中显示,这可能是由于应用程序读取了旧数据库。 每次更新数据库时,我都会更改DATABASE_VERSION
,但这似乎无济于事。
我相信问题与我的onUpgrade()
方法有关,但我不确定要在其中放置什么。
DBHelper类:
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "BB2SoulDatabase.db";
public String DB_PATH ="";
private static final int DATABASE_VERSION = 2;
public static final String TABLE_NAME = "SOULS";
public static final String SOUL_COLUMN_NAME = "Name";
private final Context myContext;
private SQLiteDatabase myDataBase;
public DBHelper(Context context) {
super(context, DATABASE_NAME , null, DATABASE_VERSION);
this.myContext = context;
DB_PATH = myContext.getDatabasePath(DATABASE_NAME).getPath();
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if(dbExist){ }
else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch(SQLiteException e) { }
if(checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
String outFileName = DB_PATH;
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 open() throws SQLException {
try {
createDataBase();
} catch (IOException e) {
throw new Error("\n" +
"It was impossible to create the database");
}
String myPath = DB_PATH;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
public Cursor getAllItems(String sortBy, String sortByAffinity, String sortByRace) {
SQLiteDatabase db = this.getReadableDatabase();
String orderBy = " DESC";
String affinity = "";
String race = "";
String raceAnd = " WHERE";
if(sortByAffinity.equals("Quickstrike")) {
affinity = " WHERE Quickstrike = 'y'";
raceAnd = " AND";
}
else if(!sortByAffinity.equals("Affinity/All")){
affinity = " WHERE Affinity = '"+sortByAffinity+ "'";
raceAnd = " AND";
}
if(!sortByRace.equals("Race/All")){
race = raceAnd+" Race = '"+sortByRace+ "'";
}
if(sortBy.equals("Name")) orderBy = " ASC";
Cursor res = db.rawQuery( "SELECT * FROM " + TABLE_NAME + affinity + race + " ORDER BY "+ sortBy + orderBy , null );
return res;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
您的代码会绕过SQLiteOpenHelper
类的常规创建/升级框架,因此您必须自己处理升级。 (要么删除旧数据库并复制新版本,要么手动升级数据库,具体取决于您是否需要保留旧数据。)
最好使用像SQLiteAssetHelper这样的库,它使您更轻松。
如果您具有只读数据库(即,您知道不需要保留旧文件),只需更改文件名 。 这样可以确保您可以简单地复制新文件(如果尚不存在)。 (如果存在,请不要忘记删除旧文件。)
场景:
您保存在外部目录中的数据库不是您的应用程序直接使用的数据库。 它在指定的路径中创建自己的数据库,并仅从资产文件夹的数据库中复制其信息。
问题:
在您的代码中,仅当一个数据库不存在时才创建(并复制)该数据库(一个应用程序直接使用)。 使用sqlite浏览器创建新数据库时,会将其保留在外部目录中。 但是应用程序使用的数据库已经存在。 因此,它不会从外部目录的新数据库中复制新数据。
解:
卸载以前安装的应用程序,然后在对数据库进行更改时安装新的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.