【问题标题】:Returning dates in pyodbc using the cursor.execute select command使用 cursor.execute select 命令在 pyodbc 中返回日期
【发布时间】:2012-01-09 19:51:50
【问题描述】:

我正在尝试使用 pyodbc 从 Oracle 数据库中返回符合某些条件的日期。例如,我想获取 2010 年 1 月 1 日到 2011 年 1 月 1 日之间的所有日期。 为此,我正在这样做:

x = cursor.execute("""选择 表中的列,其中 OutputDate > '2010/01/01' 和 OutputDate

当我使用 Access 数据库时,类似的东西起作用了,但现在它给了我这个错误:

pyodbc.DataError: ('22007', '[22007] [Microsoft][ODBC driver for Oracle][Oracle]ORA-01861: literal does not match format string (1861) (SQLExecDirectW)')

我在 Windows 7 上,我使用的驱动程序是 Microsoft ODBC for Oracle(我猜这从错误中很明显)

“文字与格式字符串不匹配”是否意味着我不能在此数据库中使用 日期?谁能帮帮我?

非常感谢, 亚历克斯

【问题讨论】:

    标签: python oracle datetime pyodbc


    【解决方案1】:

    Literal does not match format string 表示您正在尝试比较日期和字符串。将您的查询更改为以下内容,它应该可以工作。

    select column
      from table 
     where outputdate between to_date('2010/01/01','yyyy/mm/dd') 
           and to_date('2011/01/01','yyyy/mm/dd')
    

    当然,除非 outputdate 不是日期,在这种情况下,最好将其转换为日期,以便进行比较。

    注意between 运算符,您可以在这种情况下使用它。

    有一个 Oracle 数据类型列表here;该网站对一切都有好处。

    我应该补充一点,我更喜欢 cx_Oracle 从 python 与 Oracle 通信;虽然这只是个人喜好。

    【讨论】:

    • 完美!我将其替换为 x = cursor.execute("""select Column from Table where OutputDate between to_date('%s/12/31','yyyy/mm/dd') 和 to_date('%s/01/01' ,'yyyy/mm/dd')""" % (yr1, yr2)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2019-07-08
    • 1970-01-01
    相关资源
    最近更新 更多