简体   繁体   English

调用“创建表”来使数据库保持最新是一种好习惯吗?

[英]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.

相关问题 在数据库表或内存中跟踪在线用户是个好主意吗? - Is it a good idea to keep track of online users in a database table or in memory? 如何创建我的工作对象并使其保持最新状态? - How to create my work object and keep it up to date? 使用作业作为表达方式是好习惯吗? - Is using an assignment as an expression good practise? 为什么每个I / O操作都创建一个新的Stream实例是一种好习惯? (C#) - Why is it a good practise to create a new instance of Stream per each I/O operation? (C#) 使用数据库最佳实践? - working with database Best practise? 使用表内的JSON字段来加快一些关系数据库查询-一个好主意吗? - Is using JSON field inside table, to speed up some relationship database queries - a good idea? 我的用户如何使.NET应用程序中的sqlCE数据库保持最新状态? - How do my users keep their sqlCE database in .NET application up-to-date? 在生产中自我托管wcf服务是一种很好的做法 - is it a good practise to self host wcf service in production 有条件的包含是否取决于当前页面是一种很好的做法? - Is conditional includes depending on the current page a good practise? 存储带有敏感数据的文档有哪些好的做法 - What are the good practise for storing document with sensitive data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM