0

我在 ormlite 中使用数据库。我想在 Activity 中获取我的数据库版本,但我不知道在哪个表中保存版本。

有人知道我怎样才能得到数据库的版本吗?我在手册中找过,但没有找到。

非常感谢。

4

2 回答 2

3

在 onCreate 中写下这段代码

int version = getHelper().getReadableDatabase().getVersion();

希望这有帮助:)

于 2012-09-27T09:33:23.617 回答
1

如果您在谈论由 Android 操作系统管理的数据库版本号,那么您将知道该编号是什么,否则您将接到onUpgrade(...).

如果您的onUpgrade(...)方法被调用,您知道编号是多少,因为您获得了旧版本号和新版本号。如果您没有接到电话,那么您知道您在数据库助手中设置并传递给SQLiteOpenHelper类的版本号对应于 Android 存储中的版本号。

下面是一个典型ORMLite DatabaseHelper类的代码示例:

private static final int DATABASE_VERSION = 4;
...

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
}
...

/**
 * This is called when your application is upgraded and it has a higher version 
 * number. This allows you to adjust the various data to match the new version
 * number.
 */
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
    int oldVersion, int newVersion) {
    ...
于 2012-09-04T16:30:27.760 回答