简体   繁体   English

调用sqlite onUpgrade问题

[英]Calling sqlite onUpgrade problems

I have 2 databases. 我有2个数据库。 1 is a MySQL where I use a games API to gather the up-to-date information about a game. 1是MySQL,我使用游戏API来收集有关游戏的最新信息。 The other database I have is a sqlite DB that I created in my app (both have the same tables and columns). 我拥有的另一个数据库是我在我的应用程序中创建的sqlite数据库(两者都有相同的表和列)。 How I want this to work is, the app starts then the MySQL version will be checked (updated whenever the game I play has an update) I check to see if the MySQL version matches the sqlite version and update when needed. 我希望这个工作方式是,应用程序启动然后将检查MySQL版本(每当我玩的游戏有更新时更新)我检查MySQL版本是否与sqlite版本匹配并在需要时更新。

The confusion I am having is I do not want the users to have to update the entire app when a minor MySQL DB change happens (An item stat gets changed, an item gets added, a character gets added or the stat of an ability of a character gets tweaked) because what I am seeing on here and in Vogella tutorials is that it seems I need to actually change manually: 我遇到的困惑是我不希望用户在发生次要MySQL数据库更改时必须更新整个应用程序(项目状态变更,项目被添加,角色被添加或者能力属性为因为我在这里和Vogella教程中看到的是,我似乎需要手动更改:

private static final int DATABASE_VERSION = 1;  //<<<<<Manually change this
...

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

which defeats the purpose I am trying to accomplish. 这违背了我想要达到的目的。

1) Is there a way to update the DATABASE_VERSION without actually having to manually change it in the code? 1)有没有办法更新DATABASE_VERSION而不必在代码中手动更改它?

2) Can I just straight out call onUpgrade(db, oldVersion, newVersion) and put the parameters I want or is this a very bad idea and it should be handled through getWritableDatabase() ? 2)我可以直接调用onUpgrade(db,oldVersion,newVersion)并输入我想要的参数,这是一个非常糟糕的主意,应该通过getWritableDatabase()来处理吗?

The database version is changed only, when the table structure has changed, ie tables are added or deleted or when some columns change, being added or removed. 仅当表结构发生更改(即添加或删除表或某些列发生更改,添加或删除)时,才会更改数据库版本。 When this is the case and a new version of your app is installed, then Android detects the new database version and calls onUpgrade . 如果是这种情况并且安装了新版本的应用程序,则Android会检测到新的数据库版本并调用onUpgrade

If you just update the data in your tables, there is no change in the table structure and therefore no need to increase the database version number. 如果只更新表中的数据,则表结构不会发生更改,因此无需增加数据库版本号。

In your case, you can add some serial number or timestamp in one of your columns, which identifies the current data version and compare that against the version or timestamp stored in the corresponding SQLite database table. 在您的情况下,您可以在其中一列中添加一些序列号或时间戳,这些列标识当前数据版本并将其与存储在相应SQLite数据库表中的版本或时间戳进行比较。

When these differ, you can update the data from the MySQL database. 当这些不同时,您可以更新MySQL数据库中的数据。

Update : 更新

When you update your app, but the table structure is unchanged, then the database version should stay the same and the database and its data remains untouched. 更新应用程序但表结构未更改时,数据库版本应保持不变,数据库及其数据保持不变。

When you update your app and the table structure changes, you increase the database version number. 更新应用程序并更改表结构时,会增加数据库版本号。 Android compares the current and the new version and calls onUpgrade . Android比较当前版本和新版本,并调用onUpgrade Now you can do the necessary steps to upgrade your database, change the database layout, populate new tables or columns, whatever needs to be done. 现在,您可以执行必要的步骤来升级数据库,更改数据库布局,填充新表或列,以及需要执行的任何操作。 You can even destroy everything and build a new database from scratch. 您甚至可以从头开始销毁所有内容并构建新数据库。

All this is independent from the day to day update, when your app runs and pulls current data from the main server database. 所有这些都与日常更新无关,当您的应用程序运行并从主服务器数据库中提取当前数据时。

It sounds like you are reading your schema from a central MySQL database and simply keeping a local copy on the Android device. 听起来您正在从中央MySQL数据库中读取您的架构,只是在Android设备上保留本地副本。 When you change the central database's schema, yes, it's silly to manually change the local copy. 当您更改中央数据库的架构时,是的,手动更改本地副本是愚蠢的。

1) Is there a way to update the DATABASE_VERSION without actually having to manually change it in the code? 1)有没有办法更新DATABASE_VERSION而不必在代码中手动更改它?

DATABASE_VERSION is just like any other variable, if you want to control it's value from an outside source, you can. DATABASE_VERSION就像任何其他变量一样,如果你想从外部源控制它的值,你可以。 Honestly you don't even need this variable, it is only used in SQLiteOpenHelper's constructors : 老实说,你甚至不需要这个变量,它只在SQLiteOpenHelper的构造函数中使用

SQLiteOpenHelper(Context context, String name, 
        SQLiteDatabase.CursorFactory factory, int version)

SQLiteDatabase maintains it's own private version number. SQLiteDatabase维护它自己的私有版本号。

2) Can I just straight out call onUpgrade(db, oldVersion, newVersion) ...? 2)我可以直接调用onUpgrade(db,oldVersion,newVersion)......?

I don't recommend doing this because you will not be changing the internal version number and skipping other internal checks used by SQLiteOpenHelper. 我不建议这样做,因为您不会更改内部版本号并跳过SQLiteOpenHelper使用的其他内部检查。
Yes, you can perform many of these checks manually, but for every work-around that you create you are making SQLiteOpenHelper less useful. 是的,您可以手动执行许多这些检查,但是对于您创建的每个解决方法,您都会使SQLiteOpenHelper变得不那么有用。 At some point you are better off calling Context#openOrCreateDatabase() directly since you will have created your own version control code. 在某些时候,您最好直接调用Context#openOrCreateDatabase() ,因为您将创建自己的版本控制代码。

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

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