[英]How to Update row in sqlite.net pcl in windows 10 C#?
I have ID of Row then I will update other values. 我有行ID,然后我将更新其他值。
I don't know how to update my values! 我不知道如何更新我的价值观! My Table: 我的表:
class MyTable
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Date { get; set; }
public string Volumes { get; set; }
public string Price { get; set; }
}
other information: 其他信息:
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>();
}
I recently started working with UWP apps, and also came across this problem. 我最近开始使用UWP应用程序,也遇到了这个问题。 So, how to update a row using SQLite in UWP you ask? 那么,如何在UWP中使用SQLite更新行? Here ya go! 你去吧!
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);
});
}
}
The App.DB_PATH is the same as your 'path' variable. App.DB_PATH与'path'变量相同。 I realize that there are many different areas of interest in this answer, so if you have any further questions, feel free to ask. 我意识到这个答案有很多不同的兴趣领域,所以如果你有任何进一步的问题,请随时提出。
To only update a specific set of values in a row, then executing a raw SQL query would be simpler: 要仅更新行中的一组特定值,则执行原始SQL查询会更简单:
conn.Execute("UPDATE MyTable SET Price = ? Where Id = ?", 1000000, 2);
This assumes the input you are passing to the execute statement has been cleaned. 这假定您传递给execute语句的输入已被清除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.