简体   繁体   English

更新apk版本后删除了Android Sqlite数据库记录

[英]Android Sqlite Database record deleted after updating the apk version

I have tried to implement the persistence layer of my application so that after updating the apk via Google Play, the database record can be maintained. 我尝试实现应用程序的持久层,以便在通过Google Play更新apk之后,可以维护数据库记录。 When it comes ti the implementation, it erases all data after updating my apk version. 当实现时,它将在更新我的apk版本后删除所有数据。 Would you please tell me the way to back up and restore the sqlite records? 您能告诉我备份和还原sqlite记录的方法吗?

The below is my code 下面是我的代码

 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);
        }

    }

Your onUpgrade function is dropping all the tables. 您的onUpgrade函数将删除所有表。 That means you'll drop all the data. 这意味着您将删除所有数据。 If you want to not drop them, you have to write alter statements for each possible upgrade instead. 如果不想删除它们,则必须为每个可能的升级编写alter语句。

You just have to use a check if you are upgrading your database version 您只需要检查一下是否要升级数据库版本

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
 }

as you are writing this code in your db 当您在数据库中编写此代码时

    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");        

it drops your table and data 它会丢弃您的表和数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM