【问题标题】:QDateTime with sqlite3QDateTime 与 sqlite3
【发布时间】:2016-11-09 15:43:11
【问题描述】:

我将 Sqlite3 与 Qt 一起使用,无论如何要将 DateTime 表单保存在我使用 Text 类型的 db 中,请从我的 db 中查看: data

所以 INSERT 和 SELECT 非常有效,但我如何才能做出特定的 SELECT !

我的代码:

QString("SELECT * from main.sell_cash_log WHERE 'when' >= '%1' AND 'when' <= '%2'").arg(ui->fromdate->dateTime().toString("dd-MM-yyyy:HH-mm-ss")).arg(ui->todate->dateTime().toString("dd-MM-yyyy:HH-mm-ss"))

【问题讨论】:

  • 您希望查询选择特定日期的信息吗?
  • 你能写出你想得到什么和你得到什么吗?
  • @Marco select rows from db ,其中行中的日期是我选择的日期之间(fromdate - todate)

标签: c++ qt sqlite


【解决方案1】:

您最好使用日期运算符之一来获取特定日期的信息

https://www.tutorialspoint.com/sqlite/sqlite_data_types.htm

在 11 月份全选:

SELECT * FROM main.sell_cash_log WHERE strftime('%Y-%m-%d', when) BETWEEN "11-01-2016" AND "11-31-2016"

另请参阅SQL Select between dates,这是我复制该查询 q 的位置

【讨论】:

  • 我用过:CartItems-&gt;setQuery(QString("SELECT * from main.sell_cash_log WHERE strftime('%Y-%m-%d %H:%M:%S','when') BETWEEN '%1' AND '%2'").arg(ui-&gt;fromdate-&gt;dateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(ui-&gt;todate-&gt;dateTime().toString("yyyy-MM-dd hh:mm:ss")));,但还是不行,注意我用QSqlQueryModel这个查询和QTableView来查看查询
  • 我也试过CartItems-&gt;setQuery(QString("SELECT * from main.sell_cash_log WHERE date('when') BETWEEN date('%1') AND date('%2')").arg(ui-&gt;fromdate-&gt;dateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(ui-&gt;todate-&gt;dateTime().toString("yyyy-MM-dd hh:mm:ss")));
  • 您能否验证您的日期输出的字符串是否正确?
  • img 那是我的数据库数据,那是查询的 qdebug:"SELECT * from main.sell_cash_log WHERE strftime('%Y-%m-%d%H:%M:%S','when') BETWEEN '2015-11-0900:00:00' AND '2016-11-0900:00:00'"
  • 小时和天之间需要一个空格。 '2015-11-0900:00:00' 应该是 '2015-11-09 00:00:00'
【解决方案2】:

问题出在名为 when 的字段上,在插入查询中,我使用了转义字符串('when'),但 select 不起作用(''),所以我使用了(when)并且它有效: :

CartItems->setQuery(QString("SELECT * from main.sell_cash_log WHERE datetime(`when`) BETWEEN datetime('%1') AND datetime('%2')").arg(ui->fromdate->dateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(ui->todate->dateTime().toString("yyyy-MM-dd hh:mm:ss")));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2013-12-06
    • 2020-06-27
    • 1970-01-01
    相关资源
    最近更新 更多