Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a List of String in a class intended for SQLite?

What limitations are there on data types used in a class that will be used by SQLite-net to represent a table? Specifically, can I use this:

public List<string> CoconutWaterBrands { get; set; }

...or will I need this:

public string[] CoconutWaterBrands { get; set; }

...or something else altogether?

like image 812
B. Clay Shannon-B. Crow Raven Avatar asked Feb 02 '13 16:02

B. Clay Shannon-B. Crow Raven


People also ask

Can I use VARCHAR in SQLite?

SQLite does not enforce the length of a VARCHAR. You can declare a VARCHAR(10) and SQLite will be happy to store a 500-million character string there. And it will keep all 500-million characters intact. Your content is never truncated.

How do I create a list in SQLite?

You can't insert ("billy", "jim") as a column in the database. This is intentional. The whole point of RDBMSs like sqlite is that each field holds exactly one value, not a list of values. You can't search for 'jim' in the middle of a column shared with other people, you can't join tables based on 'jim ', etc.

Does SQLite is case insensitive?

For one thing, databases vary considerably in how they handle text; for example, while some databases are case-sensitive by default (e.g. Sqlite, PostgreSQL), others are case-insensitive (SQL Server, MySQL).

Can SQLite handle multiple connections?

SQLite does support multiple concurrent connections, and therefore it can be used with a multi-threaded or multi-process application. The catch is that when SQLite opens a write transaction, it will lock all the tables.


2 Answers

Similar to Sandy's Answer, in terms of serializing/deserializing the list, you could use Text blobbed properties from the SQLite-Net Extensions library. So for example in your Model class:

public class SomethingToDoWithCoconuts
{
    [TextBlob(nameof(CoconutWaterBrandsBlobbed))]
    public List<string> CoconutWaterBrands { get; set; }
    public string CoconutWaterBrandsBlobbed { 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.

like image 184
Iain Smith Avatar answered Sep 29 '22 19:09

Iain Smith


ORMs (aka abstraction leak) will have this kind of problem. The fields in the class will correspond to columns in the table. If you use List or arrays then the ORM will either have to somehow combine those values in the list in a single column, for that it will have to choose some kind of separator and what if the separator appears inside your string value, ultimately you will slowly find your self in rabbit hole.

I don't think SQLite-net support arrays/list. You will need to use string (and split and join it as required) or create a new table to represent the one-to-many relationship.

like image 23
Ankur Avatar answered Sep 29 '22 20:09

Ankur