简体   繁体   English

在每个应用程序发布 Xamarin Forms 后更新/维护 SQLite 数据库

[英]Updating/Maintaining SQLite database after each App Release Xamarin Forms

This is my first time working on a Xamarin App and I am new to the app development world so I need some help figuring out this process.这是我第一次使用 Xamarin 应用程序,我是应用程序开发领域的新手,所以我需要一些帮助来弄清楚这个过程。 Currently I run a php web service that generates some SQL files that I run in DB Browser and I get a database file which I then put into my Assets and Resources Folder.目前我运行一个 php web 服务,它生成一些我在 DB Browser 中运行的 SQL 文件,然后我得到一个数据库文件,然后将其放入我的资产和资源文件夹中。 Using each platform's API I copy the database into a writable folder and use that to run my queries.我使用每个平台的 API 将数据库复制到一个可写文件夹中,并使用它来运行我的查询。

I followed this really helpful tutorial and it worked perfectly fine.我遵循了这个非常有用的教程,它运行得非常好。 https://medium.com/@hameedkunkanoor/creating-a-sqlite-databse-and-storing-your-data-in-your-android-and-ios-application-in-xamarin-2ebaa79cdff0 . https://medium.com/@hameedkunkanoor/creating-a-sqlite-databse-and-storing-your-data-in-your-android-and-ios-application-in-xamarin-2ebaa79cdff0 After the "initial" setup I store a timestamp in a local table and and the next time the user opens the app I pass that timestamp and retrieve data that is older than that timestamp.在“初始”设置后,我将时间戳存储在本地表中,下次用户打开应用程序时,我传递该时间戳并检索早于该时间戳的数据。 The I update that timestamp and continue the process. I 更新该时间戳并继续该过程。 That data is sent back in JSON format and make the updates to the tables.该数据以 JSON 格式发回并对表进行更新。

My only concern is if a new version were to come out where I add a new table or a new column which is not present in the current version of my Database, how should I take care of those update Web Service calls?我唯一担心的是,如果出现新版本,我添加了当前版本的数据库中不存在的新表或新列,我应该如何处理这些更新 Web 服务调用? Is there a way of monitoring my DB version?有没有办法监控我的数据库版本? I read somewhere where I could just ignore the new data that is not present already, like table or columns, but I'm not really sure how to do that.我在某个地方读到了可以忽略不存在的新数据的地方,例如表或列,但我不确定如何做到这一点。 I also saw that if I call CreateTable on my current tables I could potentially update them?我还看到,如果我在当前表上调用 CreateTable,我可能会更新它们?

Also for future reference each time I develop a new app would I need to regenerate a new database file to store in the assets/resources folder?也为了将来每次开发新应用程序时的参考,我是否需要重新生成一个新的数据库文件以存储在 assets/resources 文件夹中? Is there a more automated process for this?是否有更自动化的流程? Along with monitoring the version of my database?随着监控我的数据库的版本?

Any Help/Tutorials/Suggestions would be greatly appreciated.任何帮助/教程/建议将不胜感激。

You have to remember that CreateTable it's already doing the columns update for you, because internally it calls a method called MigrateTable which you can see here for further clarification: https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs#L562 .您必须记住 CreateTable 它已经在为您更新列,因为它在内部调用了一个名为 MigrateTable 的方法,您可以在此处查看以进一步说明: https : //github.com/praeclarum/sqlite-net/blob/master/ src/SQLite.cs#L562

However you could have to handle more advanced modification to your database, like adding triggers or something similar.但是,您可能必须对数据库进行更高级的修改,例如添加触发器或类似的东西。 In that case i suggest you to perform modifications manually.在这种情况下,我建议您手动执行修改。

In Xamarin Forms i've ended up with this:https://gist.github.com/matpag/b2545cc22c8e22449cd7eaf6b4910396在 Xamarin 表单中,我最终得到了这个:https ://gist.github.com/matpag/b2545cc22c8e22449cd7eaf6b4910396

Could not be the best strategy ever but seems to work for me.不可能是有史以来最好的策略,但似乎对我有用。

Summarizing :总结
You have to save the database version in an internal flag of the SQlite database called user_version accessible with PRAGMA keyword.您必须将数据库版本保存在名为 user_version 的 SQlite 数据库的内部标志中,可使用 PRAGMA 关键字访问。 Every time you get the database connection, you have to perform a check and see if the current database version is the same as the app last database version.每次获得数据库连接时,都必须执行检查,看看当前数据库版本是否与应用程序上一个数据库版本相同。 If not you need to perform a database update and set the new current version.如果不是,您需要执行数据库更新并设置新的当前版本。

Reference here .参考这里

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

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