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 Answers 2

1

The answer in this question thread should solve your problem.

You should use .format() instead.

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

Comments

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

1 Comment

Can you please mark this answer as solution then? :)

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.