1

I am trying to use mysql functions date time function str_to_date in python. I need a guide on how to use mysql functions in python. Raw_Data is the name of the table.

sql = """INSERT INTO RawData(DateTime) VALUES ('%s')""" % (STR_TO_DATE(date_time,'%d/%b/%Y:%T'))     
cursor.execute(sql)

error:

NameError: name 'STR_TO_DATE' is not defined
8
  • 1
    that's a mysql function. what is it doing in your python code? you can't do that. Commented Mar 3, 2014 at 14:39
  • Don't be confused about where the calls happen. If you call a MySQL function, it'll run on the database server, not in Python. You can call it like a stored procedure in Python, but it'll execute on MySQL. Commented Mar 3, 2014 at 14:41
  • using it to extract date time object from string.I think it should return datetime object Commented Mar 3, 2014 at 14:42
  • @duffymo is suggesting that you wrap that all in quotes so that it is executed on the database server -- sql = """INSERT INTO RawData(DateTime) VALUES ('%s')"""% ("STR_TO_DATE(date_time,'%d/%b/%Y:%T')") cursor.execute(sql) Commented Mar 3, 2014 at 14:43
  • is there any way of using mysql functions in python. Commented Mar 3, 2014 at 14:46

2 Answers 2

2

First thing first: Never but Never use string formatting on a string and then pass the string to an SQL.

you can see the full explanation here http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters (although it's for PostgreSQL this is a ground rule for DB programming). Now want you want to do is to pass the MYSQL function as a parameter to the query you can see more about how to do this in this post:

Python MySQL Parameterized Queries and here: http://mysql-python.sourceforge.net/MySQLdb.html#cursor-objects now when you bind a date you wont need to convert it into a string and then back again. so this code will be ok:

import datetime
d = datetime.datetime.now()
sql = """ INSERT INTO RawData(DateTime) Values ('%s')"""
cursor.execute(sql, (d,))

Now if you do wish to use a function in your code the function must be part of your sql not a parameter (or passed as a string parameter which is bad). This pesudo code should like this (I haven't ran it):

sql = """INSERT INTO RawData(DateTime) VALUES (STR_TO_DATE('%s',%%d/%%b/%%Y:%%T'))"""
cursor.execute(sql, (d,))
Sign up to request clarification or add additional context in comments.

2 Comments

Very good explanation. Parameters should always be passed as parameters!
Just a note: the second parameter of execute should be a sequence (tuple or list): so it should be cursor.execute(sql, (d,)) or cursor.execute(sql, [d]) - initd.org/psycopg/docs/…
1

change your query to:
UPDATE:

sql = """INSERT INTO RawData(DateTime) VALUES (STR_TO_DATE(%s, '%%d/%%b/%%Y:%%T'))"""% (date_time)

2 Comments

Even date format may be put in the SQL string: sql = """INSERT INTO RawData(DateTime) VALUES (STR_TO_DATE(%s, '%%d/%%b/%%Y:%%T'))"""% (date_time,)
It worked for me.I was using str_to_date function of mysql in the parameter.Got the mistake.thanks!

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.