How can I get the actual datetime values from a SQLite table?

旧巷老猫 提交于 2019-12-11 19:29:48

问题


I am querying a SQLite table which has a DateTime member:

public class PhotraxBaseData
{
    [SQLite.PrimaryKey] 
    [SQLite.AutoIncrement]
    public int Id { get; set; }
    . . .
    public DateTime dateTimeTaken { get; set; }
    . . .

The records in the table contain dateTimeTaken values ranging from 2008 to 2014. None of the records contain a null or empty dateTimeTaken value.

When queried via LINQPad, I get "2008-04-25 10:38:56" for this query:

SELECT MIN(dateTimeTaken) FROM PhotraxBaseData

....and "2014-04-27 19:26:09" for "SELECT MAX(..."

IOW, LINQPad gives me what I would expect. Yet, when I run this code:

dateFrom.Date = PhotraxSQLiteUtils.GetEarliestDate();
dateTo.Date = PhotraxSQLiteUtils.GetLatestDate();
 . . .
internal static DateTimeOffset GetEarliestDate()
{
    DateTimeOffset dto;
    using (var db = new SQLite.SQLiteConnection(App.DBPath))
    {
        string sql = "SELECT MIN(dateTimeTaken) FROM PhotraxBaseData";
        dto = db.ExecuteScalar<DateTimeOffset>(sql);
    }
    return dto;
}

Note: the GetLatestDate() method is identical to the GetEarliestDate() method except for using MAX insted of MIN

...in both cases, what is actually returned from the methods is "1/1/0001" and what is displayed in the data controls is "1/1/1914". The data controls are declared this way:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="Photos taken between">
    </TextBlock>
    <DatePicker x:Name="dateFrom">
    </DatePicker>
</StackPanel>
<StackPanel Orientation="Horizontal">
    <TextBlock Text="and">
    </TextBlock>
    <DatePicker x:Name="dateTo">
    </DatePicker>

Why am I not getting the right values? And how can I get the actual MIN and MAX dates (instead of "1/1/0001" returned and "1/1/1914" displayed, to populate the DateTimeOffset variable and subsequently the DatePicker XAML control?

NOTE: I see in SQLite Browser that the only data types that seem to be available when creating a SQLite table are Integer, Text, BLOB, Real, and Numeric

So maybe my problem is that the DateTime member of the class (dateTimeTaken) does not have a corresponding data type in the SQLite table, and is thus being stored as...what? Text? Is the appropriate way to handle this to change the data type in the class from DateTime to String, and then make sure the data is stored as "20141103_111111" (yyyymmdd_hhmmss) or some such, for accurate sorting?


回答1:


I think you need to specify the exact type being returned by the database, then convert that on the client side once you've got it.

Since the value being returned could be of any type, it's boxed into an object. To unbox it, you must first cast it to the exact type that was originally boxed. Then you convert it to whatever type you need.

In other words, specify DateTime if that's the value you're getting from the database:

internal static DateTimeOffset GetEarliestDate()
{
    DateTimeOffset dto;
    using (var db = new SQLite.SQLiteConnection(App.DBPath))
    {
        string sql = "SELECT MIN(dateTimeTaken) FROM PhotraxBaseData";
        dto = (DateTimeOffset)db.ExecuteScalar<DateTime>(sql);
    }
    return dto;
}

Also, I don't know if converting the value to a DateTimeOffset is what you want to do. You might just want to change the method's return type to DateTime.



来源:https://stackoverflow.com/questions/26726074/how-can-i-get-the-actual-datetime-values-from-a-sqlite-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!