0

When I retrieve data from Mysql using datetime the below error occurred
TypeError: not enough arguments for format string. This is my code written in below

temp1=datetime.strptime(date1, '%Y-%m-%d')
temp2=datetime.strptime(date2, '%Y-%m-%d')
rows1=exdb.getData("admin", "SELECT count(id) as total_count from client_onboard WHERE (video_datetime BETWEEN '%s' AND '%s');" % (temp1) %(temp2)) 

2 Answers2

1

The answer in this question thread should solve your problem.

You should use .format() instead.

Saket Khandelwal
  • 327
  • 1
  • 10
0

You have to use a tuple for your string formatting:

from datetime import datetime

date1 = "2018-01-01"
date2 = "2018-02-01"

# Date parsing
temp1 = datetime.strptime(date1, '%Y-%m-%d')
temp2 = datetime.strptime(date2, '%Y-%m-%d')

rows1 = exdb.getData("admin",
    """SELECT count(id) as total_count from client_onboard
    WHERE (video_datetime BETWEEN '%s' AND '%s');""" % (temp1, temp2)
)

Warning: SQL Injection

You should not use string formatting for SQL queries where the input is untrusted -- e.g. comes from the user. This makes your code vulnurable for SQL injection. The cursor object of the Python DBAPI can handles this for you, e.g.:

cur.execute("SELECT * FROM platforms WHERE language = %s;", (platform,))
Lucas Kahlert
  • 987
  • 7
  • 16