Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you store lists of objects in SQLite.net?

Let us assume I have these two objects

class Customer {
   [PrimaryKey]
   public string id;
   [??????]
   public List<int> addresses;
}

and

class Address {
     [PrimaryKey, AutoIncrement]
     public int id;
     public string street;
     public int number;
}

Is there a way to use the SQLite.NET ORM to save the Customers object? cause I'm having a really hard time saving lists.

If there is no such way, is there some sort of event I can implement or method I can override so that when an object gets loaded code will trigger?

I was thinking something along the lines of adding [Ignore] above the list of addresses and when the event triggers I can use SQLite.net to load the ids of the addresses from another table

Thanks in advance for any help you can provide

like image 858
Cruces Avatar asked Apr 25 '16 21:04

Cruces


People also ask

Can I save a list in SQLite?

Generally, you do this by stringifying the list (with repr()), and then saving the string. On reading the string from the database, use eval() to re-create the list. Be careful, though that you are certain no user-generated data can get into the column, or the eval() is a security risk.

Does SQLite store data?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases. However, there are no restrictions on creating databases elsewhere.

Can I use SQLite with flask?

In this tutorial, you'll build a small web application that demonstrates how to use SQLite with Flask to perform basic data manipulation covering CRUD: Create, Read, Update, and Delete. The web application will be a basic blog that displays posts on the index page. Users can create, edit, and delete individual posts.

Does SQLite support .NET core?

SQLite is a self-contained and embedded SQL database engine. In . NET Core, Entity Framework Core provides APIs to work with SQLite.


1 Answers

Have a look at this answer here

You can do it with Text blobbed properties from the SQLite-Net Extensions library

So for example in your Model class:

public class Customer 
{
   [PrimaryKey]
   public string id;
   [TextBlob("addressesBlobbed")]
   public List<int> addresses { get; set; }
   public string addressesBlobbed { get; set; } // serialized CoconutWaterBrands
}

from the documentation on Text blobbed properties:

Text-blobbed properties are serialized into a text property when saved and deserialized when loaded. This allows storing simple objects in the same table in a single column.

Text-blobbed properties have a small overhead of serializing and deserializing the objects and some limitations, but are the best way to store simple objects like List or Dictionary of basic types or simple relationships. Text-blobbed properties require a declared string property where the serialized object is stored.

Text-blobbed properties cannot have relationships to other objects nor inverse relationship to its parent.

A JSON-based serializer is used if no other serializer has been specified using TextBlobOperations.SetTextSerializer method. To use the JSON serializer, a reference to Newtonsoft Json.Net library must be included in the project, also available as a NuGet package.

Hope this helps.

like image 147
Iain Smith Avatar answered Nov 15 '22 21:11

Iain Smith