1

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

JKennedy
  • 18,150
  • 17
  • 114
  • 198

1 Answers1

0

So my solution was to save all the Column values as strings and parse them at runtime.

Therefore my GenericSqliteObject looks like the following:

namespace FieldStrikeMove.PortableData.Helpers
{
    [Table("Table1")]
    public class GenericSqliteObject : Object
    {
        public GenericSqliteObject()
        {

        }

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


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


        public int? GetInt(string column)
        {
            int rint;

            if (Int32.TryParse(column, out rint))
                return rint;
            else
                return null;
        }

        public long? GetLong(string column)
        {
            long rlong;

            if (Int64.TryParse(column, out rlong))
                return rlong;
            else
                return null;
        }

        public bool? GetBool(string column)
        {
            bool rbool;

            if (Boolean.TryParse(column, out rbool))
                return rbool;
            else
                return null;
        }
        public double? GetDouble(string column)
        {
            double rdouble;

            if (Double.TryParse(column, out rdouble))
                return rdouble;
            else
                return null;
        }
        public float? GetFloat(string column)
        {
            float rfloat;

            if (float.TryParse(column, out rfloat))
                return rfloat;
            else
                return null;
        }
    }
}

And you can add any extra Get methods to this that you need

JKennedy
  • 18,150
  • 17
  • 114
  • 198