Search code examples
c#listsqlitexamarinandroid-sqlite

How to save List<KeyValuePairs<int,object>> as a SQLite table?


I have a List of key value pairs, but I'm not sure how I can save it in SQLite. I've downloaded the library, and created a database. I know how to save objects, but how do I save the List<KeyValuePair<int,object>> into a table?


Solution

  • You could save the int in a column and the object in another column. Or instead of using a keyvaluePair, you could add a int property to your object. Then you can save it something like:

        public void Insert(Product product)
        {
            using SQLiteConnection sqliteConnection = new SQLiteConnection(GetConnectionString());
            sqliteConnection.Execute("insert into Products (Name, Price, Quantity) values (@Name,  @Price, @Quantity)", product);
        }
    

    Here, I save just some properties from my product object in the db.