日期时间通过值从python到mysql [英] date time pass value from python to mysql

查看:261
本文介绍了日期时间通过值从python到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里是我的python错误:

  pyodbc.ProgrammingError:('42000',[42000] [MySQL] [ODBC 5.1 Driver] [mysqld-5.1.63rel13.4-log ]您的SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以获取在第11行(1064)附近的'%s和date_created<%s,(start,next) SQLExecDirectW))
文件d:\sasdev\datax\program\py\global.py,第33行,在< 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


解决方案

参数,但从不传递这些参数; %(start,next)部分在之外:

  cur_ca.execute(
select id,
date_created,
data
from bureau_inquiry where date_created> ; =%s和date_created<%s
%(开始,下一个)

你最好使用SQL参数,因此数据库可以准备查询并重用查询计划:

  cur_ca.execute(
select id,
date_created,
data
from bureau_inquiry where date_created> =?和date_created< ?
,(开始,下一个)

PyODBC使用用于SQL参数。


I kept having sql syntax error.. what am I missing in here in order to pull data by day.

Here's my python error :

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>
  """)

Here's the code :

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  

解决方案

You execute a query with formatting parameters but never pass these in; the % (start, next) part goes outside of the SQL query:

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

You would be better off using SQL parameters however, so the database can prepare the query and reuse the query plan:

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

PyODBC uses ? for SQL parameters.

这篇关于日期时间通过值从python到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆