JSK NS
JSK NS

Reputation: 3446

Python MySQL Error 1064 inserting datetime string

I have the following python code:

now = time.strftime('%Y-%m-%d %H:%M:%S')
#now = datetime.datetime.now()
query = """INSERT INTO bandwidth_by_second (current_time, down, up)  VALUES (%s, %s, %s)"""
data = (now, 1.0, 2.0)
cursor.execute(query, data)

My schema for this table is:

When running this, I get the following error:

_mysql_exceptions.ProgrammingError: (1064, "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 'current_time, down, up) VALUES ('2014-10-27 18:29:32', 1, 1)' at line 1")

I had thought I had formatted the date time wrong, but this post suggests otherwise.

What the heck is going on?

Upvotes: 0

Views: 1572

Answers (2)

JSK NS
JSK NS

Reputation: 3446

It had to do with the column name in the database table. I changed current_timeto currenttime and it started working.

Upvotes: 1

Shabir A.
Shabir A.

Reputation: 306

Use '%s' in quotes. It will work then :)

Upvotes: 3

Related Questions