Kumar
Kumar

Reputation: 874

Managing SQLite Database for Windows Store Apps using C#

I am using SQLite database for my Windows Store app. I completed my requirement of usage of Database with the help of MSFT samples & Blogs. Now I want to Change/Update the Data which is in the Database File..

My Insert code like this..

                   using (var db = new SQLite.SQLiteConnection(dbpath))
                    {
                        db.Insert(new ItemEpub.EpubBookList()
                        {
                            BookName = file.DisplayName,
                            BookPath = file.Path,
                        }
                        );
                        db.Commit();
                        db.Dispose();
                        db.Close();
                    }

I didn't get the Syntax & any simple information about Update the Database table. Right now my code is:

    StorageFile DataFile = await ApplicationData.Current.LocalFolder.GetFileAsync("Epub.db3");
        using (var db = new SQLite.SQLiteConnection(dbpath))
        {
            db.Update EpubBookList Set
            string sql = UPDATE EpubBookList SET LastVisitedPage = '" + lastVisistedPageIndex + "',";
            db.Dispose();
            db.Close();
        }

May be this code looks like ugly, Apart from this code give me any Suggestion on Update the Exiting row in metro apps

Upvotes: 1

Views: 1648

Answers (1)

Farhan Ghumra
Farhan Ghumra

Reputation: 15296

To update the existing record, first you need to get that object via SQL query [Query<T>(...)] or Get<T>(...) method. Then update the object properties and call Update(...) method.

using (var db = new SQLite.SQLiteConnection(dbpath))
{
        var objPerson = db.Query<Person>("select * from dbPerson where PersonId = ?", 9);

        /*** or ***/

        var objPerson = db.Get<Person>(9); // Here 9 is primary key value

        /*** update the object ***/

        objPerson.FirstName = "Matt";
        db.Update(objPerson);
}

You don't need to call db.Dispose(); & db.Close(); because you are using using.

Upvotes: 1

Related Questions