Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 日期时SQL选择错误_Python_Sql_Datetime - Fatal编程技术网

Python 日期时SQL选择错误

Python 日期时SQL选择错误,python,sql,datetime,Python,Sql,Datetime,我尝试使用python查询sql数据库,然后选择使用datetime的查询(在本例中,是指过去5分钟内的所有条目)。我遇到了一个奇怪的语法错误,无法理解语法错误的原因。很接近,但仍然无助于解决问题 错误: Traceback (most recent call last): File "file.py", line 20, in <module> cur.execute("SELECT * FROM DATA where timestamp bet

我尝试使用python查询sql数据库,然后选择使用datetime的查询(在本例中,是指过去5分钟内的所有条目)。我遇到了一个奇怪的语法错误,无法理解语法错误的原因。很接近,但仍然无助于解决问题

错误:

    Traceback (most recent call last):
      File "file.py", line 20, in <module>
        cur.execute("SELECT * FROM DATA where timestamp between {} and {};".format(fiveMinutesAgo, currentTimestamp))
    psycopg2.ProgrammingError: syntax error at or near "13"
    LINE 1: ...M DATA where timestamp between 2017-03-18 13:04:31.1...
                                                         ^
打印数据库时间戳行的10个条目:

    (datetime.datetime(2016, 12, 2, 10, 41, 8, 727398),)
    (datetime.datetime(2016, 12, 2, 10, 41, 12, 888281),)
    (datetime.datetime(2016, 12, 2, 10, 42, 5, 139231),)
    (datetime.datetime(2016, 12, 2, 10, 42, 8, 536972),)
    (datetime.datetime(2016, 12, 2, 10, 42, 11, 633446),)
    (datetime.datetime(2016, 12, 2, 10, 43, 7, 425955),)
    (datetime.datetime(2016, 12, 2, 10, 43, 12, 364544),)
    (datetime.datetime(2016, 12, 2, 10, 43, 17, 623814),)
    (datetime.datetime(2016, 12, 2, 10, 44, 4, 142015),)
    (datetime.datetime(2016, 12, 2, 10, 44, 7, 719680),)

感谢您的帮助

永远不要使用
格式
将值注入SQL语句。使用占位符:

cur.execute("SELECT * FROM DATA where timestamp between %s and %s", (fiveMinutesAgo, currentTimestamp))

我删除了不兼容的数据库标记。请标记您真正使用的数据库。
cur.execute("SELECT * FROM DATA where timestamp between %s and %s", (fiveMinutesAgo, currentTimestamp))