【发布时间】:2016-02-11 23:33:27
【问题描述】:
我正在创建一个使用 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) {
}
}
【问题讨论】:
-
而不是使用
onCreate()和onUpdate()从 assets 文件夹中复制数据库。只需在复制新数据库之前删除旧数据库。然后在您的首选项中存储一个值,告诉您新数据库已经被复制,所以不要一直复制它。