0

I'm getting an error replacing the %s to a date string.

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

If i replace the equals to a literal string = '20170327' it does work perfectly.

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
  • 1
    Is this a similar problem? I think you need to use strftime to coerce the value into the right format. ISO 8601 is preferred: YYYY-MM-DD. Commented Mar 28, 2017 at 23:50

1 Answer 1

-1

How about ...and date = '{}' group by date, stp_dpc".format(new_date)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.