Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have ID of Row then I will update other values.

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>();
    }
like image 362
radin Avatar asked Nov 04 '15 10:11

radin


People also ask

How do I edit data in SQLite database?

Introduction to SQLite UPDATE statement First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause. The WHERE clause is optional.

How do I change a column value in SQLite?

In order to update a particular column in a table in SQL, we use the UPDATE query. The UPDATE statement in SQL is used to update the data of an existing table in the database.

What is SQLite net PCL?

SQLite-net is an open source and light weight library providing easy SQLite database storage for . NET, Mono, and Xamarin applications. This version uses SQLitePCLRaw to provide platform independent versions of SQLite. Product.


2 Answers

I recently started working with UWP apps, and also came across this problem. So, how to update a row using SQLite in UWP you ask? 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. I realize that there are many different areas of interest in this answer, so if you have any further questions, feel free to ask.

like image 142
instanceof Avatar answered Oct 23 '22 03:10

instanceof


To only update a specific set of values in a row, then executing a raw SQL query would be simpler:

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

This assumes the input you are passing to the execute statement has been cleaned.

like image 33
Jay Rainey Avatar answered Oct 23 '22 04:10

Jay Rainey