python mysql不支持的格式字符'''

时间:2017-03-28 23:31:27

标签: mysql python-2.7

我在将%s替换为日期字符串时出错。

import datetime

now = datetime.datetime.now()

day_minus_one = now + datetime.timedelta(days=-1)
new_date = day_minus_one.strftime('%Y%m%d')

db = MySQLdb.connect(host='127.0.0.1', user='root', passwd='', db='daily_report')
cur = db.cursor()

sql = "select date, stp_dpc, count(*), concat(round(avg(status = 'Success') * 100), '%') as Success from daily where status not in ('data1', 'data2', 'data3') and date = %s group by date, stp_dpc"

cur.execute(sql, new_date)
rows = cur.fetchall()

如果我将等号替换为文字字符串=' 20170327'它确实很有效。

cur.execute("select date, stp_dpc, count(*), concat(round(avg(status = 'Success') * 100), '%') as Success from daily where status not in ('data1', 'data2', 'data3') and date = '20170310' group by date, stp_dpc")
rows = cur.fetchall()

1 个答案:

答案 0 :(得分:-1)

如何...和date ='{}'按日期分组,stp_dpc“.format(new_date)