目前我擺弄自己在Android中使用SQL數據庫並創建我的第一個應用程序。現在我想更新這個應用程序和資產文件夾中的包含數據庫。我用新的數據覆蓋了舊的數據庫,但在我的手機上它並沒有顯示新的條目。只讀SQLite數據庫更新資產文件夾中的新應用程序版本
它以某種方式保存舊的條目/查詢或完全舊的Db。我檢查了網絡,但無法真正找到解決方案。在我的dbhelper代碼下面。我需要在onUpgrade
中做些什麼或者該做什麼?
非常感謝您的幫助!
public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH;
private static String DB_NAME;
private SQLiteDatabase db_object;
private final Context context;
public DataBaseHelper(Context context, String db) {
super(context, db, null, 80);
this.context = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
// DB_PATH = "/data/data/com.comp.appname/databases/";
DB_NAME = "mydatabase.sqlite";
try {
createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
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 + DB_NAME;
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 = context.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;
db_object = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (db_object != null)
db_object.close();
super.close();
}
}
噢...好的。從DBMS刪除我們的舊數據庫並嘗試再次運行它 –
對不起。那對我不明白。你能詳細說明一下嗎?你的意思是以編程方式刪除它? – SunnySonic
好的。我想你是什麼意思,但如果我從日食中刪除它,它只會在我的手機上工作,但如果人們從市場更新我的應用程序,則不會。所以我需要一個無處不在的解決方案。 – SunnySonic