本文介绍了将版本设置为 SQLite 数据库文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C++ 应用程序,它通过 QxOrm 将数据存储在数据库 (SQLite) 中.

I have a C++ application that stores data in a database (SQLite) through QxOrm.

很明显,在下一个版本中,列将被添加/删除/重命名,所以我想为每个创建的数据库设置一个版本号,以便当有人尝试加载数据库时,它会将其版本与应用程序的当前版本并自动添加/删除/重命名列以匹配当前架构.

It is clear that in the next versions, columns are gonna be added/removed/renamed, so I would like to set a version number to each database created, so that when someone tries to load a database it compares its version with the current version of the application and automatically add/remove/rename the columns to match the current schema.

我在 QxOrm 文档中找不到类似的内容:

I couldn't find in the QxOrm documentation something that would look like:

qx::QxSqlDatabase::getSingleton()->setVersion(2);

那么首先可以用 SQLite 做那种事情吗?如果不是,我应该创建一个包含数据库版本的表吗?

So first is it possible to do that kind of thing with SQLite? and if not should I just create a table that would hold the database version?

推荐答案

一个数据库版本可能还不够:你应该为每个持久类存储一个版本(可能还有每个持久类的其他信息,例如列列表).当您将持久类注册到 QxOrm 上下文中时,您必须输入版本号:

A database version is perhaps not enough : you should store a version per persistent class (and maybe other informations per persistent class, like list of columns for example).When you register a persistent class into QxOrm context, you have to put a version number :

QX_REGISTER_HPP_XXX(myClass, myBaseClass, myClassVersion)

您可以在 QxOrm 库的 FAQ 中找到有关创建 SQL 模式的一些信息:http://www.qxorm.com/qxorm_en/faq.html#faq_230

You can find some informations about creating a SQL schema into the FAQ of QxOrm library :http://www.qxorm.com/qxorm_en/faq.html#faq_230

使用QxOrm库的内省引擎,很容易做到,更多关于内省引擎的细节在这里:http://www.qxorm.com/qxorm_en/faq.html#faq_190

Using introspection engine of QxOrm library, it's quite easy to do, more details about introspection engine here :http://www.qxorm.com/qxorm_en/faq.html#faq_190

您应该在数据库中创建一个表来存储每个持久类的状态:您可以存储每个类的版本号、列列表等......然后比较两个版本的持久化将非常容易类来修改您的 SQL 架构.

You should create a table into your database to store a state for each persistents classes : you can store a version number per class, a list of columns, etc... Then it will be quite easy to compare 2 versions of persistent class to modify your SQL schema.

这篇关于将版本设置为 SQLite 数据库文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:19