Android-SQLite-检测是否未找到新版本 [英] Android - Sqlite - Detect if new version not found

查看:85
本文介绍了Android-SQLite-检测是否未找到新版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:Bref:Kuffs的答案简单而正确. MikeT的答案是一种有趣的方法.

Updated: Bref: The answer of Kuffs is simple and correct. The answer of MikeT is a interesting approach.

旧版本: 我正在开发一个android应用程序. 我需要知道是否有新的sql版本.如果我的数据库是最新的,我将触发一个异步操作. 但是我该如何触发我的异步操作?

Old version: I'm working on an android app. I need to know if there are a new sql version or not. If my db is up to date, I will trigger an async action. But how can I trigger my async action?

当前,我有自己的标志onNewVersion,该标志在onUpgrade()中设置为true.然后在onOpen()中进行检查.还有其他方法吗?

Currently, I have my own flag onNewVersion who is set to true in onUpgrade(). Then I do the check in onOpen(). Are there any other methods?

@override
onUpgrade(...) {
    onNewVersion = true;
    ...
}

@override
onOpen(...) {
    if (onNewVersion == FALSE)
        triggerAction();
}

谢谢.

推荐答案

还有其他方法吗?

Are there any other methods?

是的,我个人使用了一种伪/期望/期望的模式,并根据需要将其与实际存在的列,表,正在创建的索引(表索引)/添加的列(列)进行比较.

Yes, I personally use a sort of pseudo/wanted/desired schema and in short compare this against what actually exists columns, tables, indexes being created(tables indexes)/added(columns) as required.

因此,我不使用版本,并且onUpgrade为空.

As such I don't use versions and onUpgrade is empty.

例如,这些是我的SQLiteOpenhelper子类中的核心方法:-

for example these are the core methods in my SQLiteOpenhelper subclass :-

    @Override
    public void onCreate(SQLiteDatabase db) {
        usable = this.onExpand(db,false);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
    }

    public boolean onExpand(SQLiteDatabase db, boolean buildandexpand) {
        boolean rv = true;
        if (db == null) {
            db = instance.getWritableDatabase();
        }
        // Is the Database definition valid to use?
        if (DBConstants.cardonsier.isDBDatabaseUsable()) {
            ArrayList<String> buildsql = DBConstants.cardonsier.generateDBBuildSQL(db);
            // Anything to build
            if (!buildsql.isEmpty()) {
                // YES so build the Database
                DBConstants.cardonsier.actionDBBuildSQL(db);
            }
            if (buildandexpand) {
                ArrayList<String> altersql = DBConstants.cardonsier.generateDBAlterSQL(db);
                if (!altersql.isEmpty()) {
                    DBConstants.cardonsier.actionDBAlterSQL(db);
                }
            }
        }
        else {
            rv = false;
        }
        return rv;
    }

DBConstants.cardoniser引用定义为:-

DBConstants.cardoniser refers to the DBDatabase object defined as :-

    static final DBDatabase cardonsier = new DBDatabase(DATABASENAME,
            new ArrayList<>(Arrays.asList(
                    DBCardsTableConstants.CARDS,
                    DBUsertableConstants.USERS,
                    DBCardUserLinkTableConstants.CARDUSERLINKS,
                    DBPreftableConstants.PREFS,
                    DBPayeestableContants.PAYEES,
                    DBCategoriestableConstants.CATEGORIES,
                    DBCardCategoryLinkTableConstants.CARDCATEGORYLINKS,
                    DBCardPayeeLinkTableConstants.CARDPAYEELINKS,
                    DBTransactionsTableConstants.TRANSACTIONS,
                    DBCardTypesTableConstants.CARDTYPES
            ))
    );

即DBTable对象的列表,它们包括DBColumn对象,例如:-

i.e. the list of DBTable objects, they including DBColumn objects e.g :-

static final DBTable CARDS = new DBTable(CARDS_TABLE,
        new ArrayList<>(Arrays.asList(
                CARDID,
                CARDOWNER,
                CARDNAME,
                CARDTYPEREF,
                CARDNUMBER,
                CARDCVVCODE,
                CARDPIN,
                CARDNOTES,
                CARDEXPIRYDATE,
                CARDNAMEONCARD
        ))
);

列定义如下:-

static final DBColumn CARDID = new DBColumn(true);
static final DBColumn CARDOWNER = new DBColumn(
        CARDOWNER_COL,
        SQLINTEGER,
        false,
        "0"
);
static final DBColumn CARDNAME = new DBColumn(
        CARDNAME_COL,
        SQLTEXT,
        false,
        ""
);
static final DBColumn CARDTYPEREF = new DBColumn(
        CARDTYPEREF_COL,
        SQLINTEGER,
        false,
        "0"
);

注意!第一列CARDID,对标准_id列使用快捷方式构造函数.

Note! the first column CARDID, uses a shortcut constructor for standard _id columns.

要添加新列,只需定义DBColumn并将DBColumn包含在DBTable定义中即可.在新列的情况下,onExpand方法将通过DBDatabase的actionDBAlterSQL方法添加该列.新表需要上述内容以及DBDatabase定义中包含的表,并由onExpand通过actionDBBuildSQL方法应用.

To add a new column it's simply a matter of defining the DBColumn and including the DBColumn in the DBTable definition. The onExpand method will add the column via, in the case of a new column, the DBDatabase's actionDBAlterSQL method. New tables require the above plus the inclusion of the table in the DBDatabase definition and are applied by onExpand via the actionDBBuildSQL method.

onExpand.但是,通过传递true以便调用actionDBAlterSQL方法.

onExpand is also called when the App(s) is started. However, with true being passed so that the actionDBAlterSQL method is called.

作为示例,添加:-

private static final String CARDCOLOUR_COL = "cardcolour";
..........
static final DBColumn CARDCOLOUR = new DBColumn(
        CARDCOLOUR_COL,
        SQLINTEGER,
        false,
        Long.toString(0x00_00_00_00_00L)
        // Flag, alpha, red, green, blue
        // Flag used to inidcate if colour has been set
);

,然后按照以下说明将CARDCOLOUR添加到DBTable定义中:-

and then adding CARDCOLOUR to the DBTable definition as per :-

static final DBTable CARDS = new DBTable(CARDS_TABLE,
        new ArrayList<>(Arrays.asList(
                CARDID,
                CARDOWNER,
                CARDNAME,
                CARDTYPEREF,
                CARDNUMBER,
                CARDCVVCODE,
                CARDPIN,
                CARDNOTES,
                CARDEXPIRYDATE,
                CARDNAMEONCARD,
                CARDCOLOUR     //<<<<<<<<<<
        ))
);

产生的结果(注意在开发时记录日志存在):-

Results in (note logging exists when developing) :-

09-22 08:30:26.802 2713-2713/? D/DBEXPAND: Expanding Database Schema Usability=true
09-22 08:30:26.803 2713-2713/? D/DBEXPAND: Build SQL is as follows:-
09-22 08:30:26.803 2713-2713/? D/DBEXPAND: ALTERSQL is as folows:-
09-22 08:30:26.807 2713-2713/? D/DBEXPAND: ALterSQL Line=    ALTER TABLE cards ADD COLUMN cardcolour INTEGER  DEFAULT 0  ;

结果已经填充的表如下所示:-

The resultant already populated table then looks like :-

使用具有DEFAULT值的ALTER TABLE( DBColumn 第4个参数(第1个是列名,第2个类型,第3个包含在主索引中的true))填充现有行的新列.

Using ALTER TABLE with a DEFAULT value (4th parameter of DBColumn (1st is column name, 2nd Type, 3rd true to include in primary index)) populates the new column for existing rows.

这篇关于Android-SQLite-检测是否未找到新版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆