0

I'm trying to run a SQL query through mysql.connector that requires a SET command in order to query a specific table:

import mysql.connector
import pandas as pd   

cnx = mysql.connector.connect(host=ip,
                              port=port,
                              user=user,
                              passwd=pwd,
                              database="")
sql="""SET variable='Test'; 
       SELECT * FROM table  """

df = pd.read_sql(sql, cnx)

when I run this I get the error "Use multi=True when executing multiple statements". But where do I put multi=True?

1
  • Possibly pd.read_sql? That seems to be the line that'll raise this error. Commented Jul 26, 2018 at 16:29

2 Answers 2

1

after many hours of experimenting, i figured out how do to this. forgive me if this is not the most succinct way, but the best i could come up with-

import mysql.connector
import pandas as pd   

cnx = mysql.connector.connect(host=ip,
                              port=port,
                              user=user,
                              passwd=pwd,
                              database="")
sql1="SET variable='Test';" 

sql2="""SELECT * FROM table  """

cursor=cnx.cursor()
cursor.execute(sql1)
cursor.close()

df = pd.read_sql(sql2, cnx)
Sign up to request clarification or add additional context in comments.

Comments

0

Pass the parameters as a dictionary into the params argument should do the trick, documentation here:

pd.read_sql(sql, cnx, params={'multi': True}) 

The parameters are passed to the underlying database driver.

1 Comment

Thanks for the help. I get the same error even when passing the params as you suggest

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.