Querying bool column always returns false
Asked Answered
F

1

7

I'm using SQLite in my project and to connect to DB I use sqlite-net library. I have created a model:

internal class Product
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    public string Name { get; set; }

...

    public bool IsDefault { get; set; }
}

There are a few records in database - some have IsDefault column value set to true, some to false. However, when I try to query records like this:

var result = dc.Table<Product>().ToArray();

every record has IsDefault set to false. Inserting a record works fine, but querying it do not.

When I changed column type to bool? in model, it returns null.

EDIT---- What is weird is when I execute query

var t = dc.Table<Product>().Where(i => i.IsDefault == true).ToArray();

It returns me correct number of records, but even so every item of array has IsDefault set to false.

Footless answered 20/8, 2013 at 12:18 Comment(4)
dc object is of type SQLiteConnection. Table - it is queryable interface for type given in brackets (it is part of sqlite-net)Footless
I just checked the sqlite-net library and found what it does, have you tried using it as an integer and casting it to bool? On the source it does not seemed to have bool defined anywhere.Thirsty
Yes, I tried to change type to int - in this case it always returns 0.Footless
change it to string and compare like i.IsDefault.ToLower().Equals("true").Sedimentation
C
2

Edited:

SqlLite has some limited types, bool is not one of them: http://www.tutorialspoint.com/sqlite/sqlite_data_types.htm

You have to use Int (0 / 1)

Coco answered 20/8, 2013 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.