[英]Is calling 'Create Table' to keep database up to date a good practise?
I am new to Xamarin and SQLite so I'm asking myself what is a good practise to keep the database structure up to date, when a new column is added or so.我是 Xamarin 和 SQLite 的新手,所以我问自己在添加新列时保持数据库结构最新的好做法是什么。 To use the SQLite DB I am using SQLite-net-pcl.
要使用 SQLite 数据库,我正在使用 SQLite-net-pcl。
I have read about some solutions that use a version field stored somewhere and that is used to alter the database structure manually when the version changes.我已经阅读了一些使用存储在某处的版本字段的解决方案,并且用于在版本更改时手动更改数据库结构。
But from what I saw is that calling CreateTable on a SQLiteConnection does not only create the table, it also updates the table in the database when the underlying class changes.但是我看到的是,在 SQLiteConnection 上调用 CreateTable 不仅会创建表,还会在底层类更改时更新数据库中的表。
So, is it a good practise just to call那么,只是打电话是一个好习惯吗?
SQLiteConnection db = new SQLiteConnection(dbPath);
db.CreateTable<ClassA>();
everytime the system initializes, to keep the database up to date?每次系统初始化时,保持数据库最新? Every change to ClassA will then be applied to the database table without any data loss.
然后,对 ClassA 的每个更改都将应用于数据库表,而不会丢失任何数据。
I test this operation, I got the following result.我测试了这个操作,我得到了以下结果。
First of all, My model like following code.首先,我的模型喜欢下面的代码。
public class Prijem
{
[PrimaryKey, AutoIncrement, Unique]
public int BCode { get; set; }
public string Name { get; set; }
public string FirmName { get; set; }
public string ItemCode { get; set; }
public string Count { get; set; }
}
I use following code to insert data.我使用以下代码插入数据。
await App.Pdatabase.SavePrijemAsync(new Prijem() {Name="New",FirmName="55Fame" ,ItemCode="dg",Count="15"});
My SavePrijemAsync
method like following code.我的
SavePrijemAsync
方法如以下代码。
public Task<int> SavePrijemAsync(Prijem prijem)
{
if (IsExisted(prijem))
{
return _database.UpdateAsync(prijem);
}
else
{
return _database.InsertAsync(prijem);
}
}
I got the record like following sceenshot in the sqlite database.我在sqlite数据库中得到了如下sceenshot的记录。
Then I just add a property called MyImage
然后我只需添加一个名为
MyImage
的属性
public class Prijem
{
[PrimaryKey, AutoIncrement, Unique]
public int BCode { get; set; }
public string Name { get; set; }
public string FirmName { get; set; }
public string ItemCode { get; set; }
public string Count { get; set; }
string image = " Image";
public string MyImage
{
set
{
if (image != value)
{
image = value;
}
}
get
{
return image;
}
}
}
I used update operation like following code.我使用了如下代码的更新操作。
await App.Pdatabase.SavePrijemAsync(new Prijem() {Name="New",FirmName="55Fame" ,ItemCode="dg",Count="15" });
And insert operation like following code.并像以下代码一样插入操作。
await App.Pdatabase.SavePrijemAsync(new Prijem() { Name = "New11", FirmName = "55Fame", ItemCode = "dg", Count = "15" });
I got the result in the database like following screenshot.我在数据库中得到了如下截图所示的结果。
In the end, We can get the result, If we add a property to the Model.最后,我们可以得到结果,如果我们向模型添加一个属性。 this column will be added in the sqlite database, but default value we must update it manually for existing data, if we insert the new value to the database, the default value will be added.
此列将添加到sqlite数据库中,但默认值我们必须为现有数据手动更新它,如果我们将新值插入数据库,则将添加默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.