1

我一直有 sql 语法错误。为了每天提取数据,我在这里遗漏了什么。

这是我的python错误:

pyodbc.ProgrammingError: ('42000', "[42000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.63rel13.4-log]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s and date_created < %s ,(start, next)' at line 11 (1064) (SQLExecDirectW)")
File "d:\sasdev\datax\program\py\global.py", line 33, in <module>
  """)

这是代码:

start = datetime.date(2012,01,01)
next = start + datetime.date.resolution

while next <= datetime.date.today():
     print start, next 

     cur_ca.execute("""
             select id,
         date_created,
         data
         from bureau_inquiry where date_created >= %s and date_created < %s %(start, next)
         """)
     start = next
     next = start + datetime.date.resolution  
4

1 回答 1

6

您使用格式化参数执行查询,但从不传递这些参数;该% (start, next)部分超出了 SQL 查询:

cur_ca.execute("""
         select id,
     date_created,
     data
     from bureau_inquiry where date_created >= %s and date_created < %s
     """ % (start, next)
   )

但是,最好使用 SQL 参数,这样数据库就可以准备查询并重用查询计划:

cur_ca.execute("""
         select id,
     date_created,
     data
     from bureau_inquiry where date_created >= ? and date_created < ?
     """, (start, next)
   )

PyODBC?用于 SQL 参数。

于 2012-09-21T18:10:39.587 回答