簡體   English   中英

如何在Windows 10 C#中更新sqlite.net pcl中的行?

[英]How to Update row in sqlite.net pcl in windows 10 C#?

我有行ID,然后我將更新其他值。

我不知道如何更新我的價值觀! 我的表:

 class MyTable
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Date { get; set; }
    public string Volumes { get; set; }
    public string Price { get; set; }
}

其他信息:

 string path;
    SQLite.Net.SQLiteConnection conn;

    public updatepage()
    {
        this.InitializeComponent();
        path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ccdb.sqlite");

        conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);

        conn.CreateTable<MyTable>();
    }

我最近開始使用UWP應用程序,也遇到了這個問題。 那么,如何在UWP中使用SQLite更新行? 你去吧!

using (var dbConn = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), App.DB_PATH))
    {
        var existingUser = dbConn.Query<User>("select * from User where Id = ?", user.Id).FirstOrDefault();
        if (existingUser != null)
        {
            existingUser.Name = user.Name;
            existingUser.Email = user.Email;
            existingUser.Username = user.Username;
            existingUser.Surname = user.Surname;
            existingUser.EmployeeNumber = user.EmployeeNumber;
            existingUser.Password = user.Password;
            dbConn.RunInTransaction(() =>
            {
                dbConn.Update(existingUser);
            });
        }
    }

App.DB_PATH與'path'變量相同。 我意識到這個答案有很多不同的興趣領域,所以如果你有任何進一步的問題,請隨時提出。

要僅更新行中的一組特定值,則執行原始SQL查詢會更簡單:

conn.Execute("UPDATE MyTable SET Price = ? Where Id = ?", 1000000, 2);

這假定您傳遞給execute語句的輸入已被清除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM