lundi 14 décembre 2015

How do I return a generic Readable object from a Sqlite Query?

I am using a query to query my Sqlite database like so:

string noteSql = "select Column1, Column2 from [Diary.Notes]"

var notes = database.Query<object>(noteSql) as List<Object>;

Where Column1 is an Int and Column2 is a String

But when my list notes is returned the objects are unreadable.

I know I can make an object with two properties of type Int and String which would work for my two columns returned from [DiaryNotes] but I would like a more generic solution.

I have tried to make a generic class like the following:

[Table("Table1")]
public class GenericSqliteObject : Object
{
    public GenericSqliteObject()
    {

    }

    private object _column1;
    [Column("Column1")]
    public object Column1
    {
        get { return _column1; }
        set { _column1 = value; }
    }


    private object _column2;
    [Column("Column2")]
    public object Column2
    {
        get { return _column2; }
        set { _column2 = value; }
    }

}

but changing the line to:

var notes = database.Query<GenericSqliteObject>(noteSql) as List<GenericSqliteObject>;

Keeps returning the error:

{System.NotSupportedException: Don't know how to read System.Object
at SQLite.Net.SQLiteCommand.ReadCol (IDbStatement stmt, Int32 index, ColType type, System.Type clrType) [0x00827] in :0
at SQLite.Net.SQLiteCommand+d__15`1[T].MoveNext () [0x00137] in :0

Is there a way to return a generic object from a Sqlite query so that I do not have to create objects for different use cases?

PS. my example here has been simplified to demonstrate the question

Aucun commentaire:

Enregistrer un commentaire