[英]Android Sqlite Database record deleted after updating the apk version
我嘗試實現應用程序的持久層,以便在通過Google Play更新apk之后,可以維護數據庫記錄。 當實現時,它將在更新我的apk版本后刪除所有數據。 您能告訴我備份和還原sqlite記錄的方法嗎?
下面是我的代碼
private static final String DATABASE_NAME = "hkgadddlden.sqlite";
private static final int DATABASE_VERSION = 6;
private Dao<History, Integer> hDao = null;
private Dao<Favourite, Integer> fDao = null;
private Dao<Lm, Integer> lDao = null;
private Dao<iconPlus, Integer> iDao = null;
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database,ConnectionSource connectionSource)
{
// database.execSQL("create table history(t_id integer, topic text, page integer, PRIMARY KEY(t_id));");
// database.execSQL("create table lm(t_id integer, PRIMARY KEY(t_id));");
// database.execSQL("create table favourites(t_id integer, PRIMARY KEY(t_id));");
try
{
TableUtils.createTable(connectionSource, History.class);
TableUtils.createTable(connectionSource, Favourite.class);
TableUtils.createTable(connectionSource, Lm.class);
TableUtils.createTable(connectionSource, iconPlus.class);
}
catch (SQLException e)
{
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
}
catch (java.sql.SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db,ConnectionSource connectionSource, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS history"); //刪除舊有的資料表
db.execSQL("DROP TABLE IF EXISTS lm");
db.execSQL("DROP TABLE IF EXISTS favourite");
db.execSQL("DROP TABLE IF EXISTS iconPlus");
onCreate(db, connectionSource);
try
{
List<String> allSql = new ArrayList<String>();
for (String sql : allSql)
{
db.execSQL(sql);
}
}
catch (SQLException e)
{
Log.e(DatabaseHelper.class.getName(), "exception during onUpgrade", e);
throw new RuntimeException(e);
}
}
您的onUpgrade函數將刪除所有表。 這意味着您將刪除所有數據。 如果不想刪除它們,則必須為每個可能的升級編寫alter語句。
您只需要檢查一下是否要升級數據庫版本
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion<2) {
// do upgrade from 1 to 2
}
if (oldVersion<3) {
// do upgrade from 2 to 3, which will also cover 1->3,
// since you just upgraded 1->2
}
// and so on
}
當您在數據庫中編寫此代碼時
db.execSQL("DROP TABLE IF EXISTS history"); //刪除舊有的資料表
db.execSQL("DROP TABLE IF EXISTS lm");
db.execSQL("DROP TABLE IF EXISTS favourite");
db.execSQL("DROP TABLE IF EXISTS iconPlus");
它會丟棄您的表和數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.