简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM