使用 Xamarin android 的 Sqlite.net Pcl 升级数据库 [英] Upgrade database with Sqlite.net Pcl for Xamarin android

查看:56
本文介绍了使用 Xamarin android 的 Sqlite.net Pcl 升级数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是,当我发布新版本的应用程序时,如果我向其中一个数据库表添加新列,则数据库不会更新.如果有新列或新表,有谁知道如何创建升级版本的脚本?

谢谢

解决方案

你必须记住 CreateTable 它已经在为你做列更新,因为它在内部调用了一个名为 MigrateTable.

但是,您可能需要对数据库进行更高级的修改,例如添加触发器或类似的东西.在这种情况下,我建议您手动执行修改.

在 Xamarin Forms 中,我最终得到了这个:https://gist.github.com/matpag/b2545cc22c8e22449cd7eaf39b6b491

不可能是有史以来最好的策略,但似乎对我有用.

总结:
您必须将数据库版本保存在名为 user_version 的 SQlite 数据库的内部标志中,可使用 PRAGMA 关键字访问.每次获得数据库连接时,都必须执行检查,看看当前数据库版本是否与应用程序上一个数据库版本相同.如果不是,您需要执行数据库更新并设置新的当前版本.

i have the problem that when i release new version of my application, if i add a new column to one of my db tables, the database doesn't update. Any one know how to create a script of upgrade versione in case there are new columns or new tables??

Thanks

解决方案

You have to remember that CreateTable it's already doing the columns update for you, because internally it calls a method called MigrateTable.

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

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. 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.

这篇关于使用 Xamarin android 的 Sqlite.net Pcl 升级数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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