【问题标题】:Retrieving data from sqlite on the basis of date根据日期从sqlite中检索数据
【发布时间】:2013-09-12 08:20:39
【问题描述】:

我正在使用 c# 中的以下查询从 Sqlite 数据库中检索数据

SELECT * FROM tableName

它工作正常。但我想根据日期检索数据,例如:

SELECT * FROM tableName WHERE date=09-09-2013

但它对我不起作用,因为 Sqlite 日期表示不是这种日期格式。 我想问的是,有什么方法可以根据上面查询中提到的用户日期和时间来检索 Sqlite 数据,以及如何以用户可读格式表示 Sqlite 数据库的日期和时间。

【问题讨论】:

  • 你可能想在 sqlite 中搜索更多这个strftime
  • 使用参数化查询。
  • 09-09-2013 是三个相减的数字。也许您想使用字符串'09-09-2013'

标签: c# sqlite datetime


【解决方案1】:

参数化查询将使您的代码从各种数据库引擎所需的日期、字符串和小数格式中解放出来

using (SqliteConnection con = new SqliteConnection(connectionString)) 
{
    con.Open();
    string commandText =  "SELECT * FROM tableName WHERE date=@dt";
    using (SqliteCommand cmd = new SqliteCommand(commandText, con))
    {
        cmd.Parameters.AddWithValue("@dt", yourDateVariable)
        SqliteReader reader = cmd.ExecuteReader();
        while(reader.Read())
        {
            // Extract your data from the reader here
            .....
        }
    }             
}

此示例的重点是展示如何构建参数化查询。通过这种方式,您将 datetime 变量的值传递给 Sqlite 引擎的框架,该引擎更了解如何为底层系统格式化日期。

commandText 变量中,日期的实际格式化值由占位符@dt 获取,然后向 SqliteCommand 参数集合添加一个与占位符名称相同的参数和日期变量中的值.

【讨论】:

    猜你喜欢
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多