Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializable data types in SQLite.net

Introduction: In a SQLite.net powered SQLite database (on WP8.1 SL, but this shouldn't matter here) I'm adding data based on a given object. This object contains a custom type called Date. Until now I don't store that property in the DB but use another property as workaround.

[Ignore]
public Date Date { get; set; }

[PrimaryKey]
public DateTime DateInternal
{
    get { return Date.ToDateTime(); }
    set { Date = new Date(value); }
}

While this works fine I feel this is not the best way to do that.

Actual Question: How can I improve that. I.e. How can I store a serialized version of Date directly. It should be in a way so that Date can be used as primary key. It is not important to me to have all the properties in Date available in single columns in a table. I want to store Date itself in just one column.

Current Research: In an attempt to Google for an answer I stumbled upon SQLite.net's ISerializable interface but I'm unsure how to use it as it only has a serialize method but no deserialize method.

namespace SQLite.Net
{
    public interface ISerializable<T>
    {
        [PublicAPI]
        T Serialize();
    }
}
like image 645
Stephan Avatar asked Jun 25 '15 07:06

Stephan


People also ask

How do I serialize objects to a SQLite database using sqliteplus?

This article explains 3 ways to serialize .NET objects to a SQLite database using SQLitePlus, which are: 1) Whole object serialization, 2) Custom object serialization, and 3) as normal database column/field level saving. The first two ways use a single binary database field (BLOB) to persist objects.

What is data type in SQLite database?

SQLite database provides the different types of data types. When we compare SQLite data type with other database systems such as MySQL and PostgreSQL it uses the static data types, which means when we declare the data type of column at the time of table creation that specified column accepts only the value of the declared data type.

What is the dynamic type system used in SQLite?

SQLite uses a more general dynamic type system. In SQLite, the datatype of a value is associated with the value itself, not with its container. The dynamic type system of SQLite is backwards compatible with the more common static type systems of other database engines in the sense that SQL statements...

What does SQLite mean?

SQLite uses a dynamic type system where the type of a value is associated with the value itself and not the column where it's stored. You're free to use whatever column type name you want. Microsoft.Data.Sqlite won't apply any additional semantics to these names. The column type name does have an impact on the type affinity.


1 Answers

Known Issue(s): there should at least be a comment in the ISerializable class stating any usage requirement(s).

  • Because there isn't, this SQLite.Net-PCL Issue was submitted. It also mentions the fix (e.g. satisfying the interface's constructor assumption)

Solution: your serializable class needs ctor that takes in the serializable type as a parameter.

An Example:

Class w/two ints:

public struct MySerializable : ISerializable<string>
{
    public int Value1 { get; set; }
    public int Value2 { get; set; }

    // ****See Here: Ctor taking serialized type to restore field vals
    public MySerializable(string serializedData) : this()
    {
        var stringVals = serializedData.Split(',');
        Value1 = Convert.ToInt32(stringVals[0]);
        Value2 = Convert.ToInt32(stringVals[1]);
    }

    public override string ToString()
    {
        return string.Format("{0},{1}", Value1, Value2);
    }

    // ****See  Here: serializing field vals to string
    public string Serialize()
    {
        return ToString();
    }
}

Being used in an SQLite-persisted class:

public class MyTable
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; private set; }

    public MySerializable MySerValues { get; private set; }
}
like image 128
bunkerdive Avatar answered Oct 07 '22 18:10

bunkerdive