MySQL Stored Procedures, Pandas, and Use multi=True when executing multiple statements

2024/10/15 3:15:57

Note - as MaxU suggested below, the problem is specific to mysql.connector and does not occur if you use pymysql. Hope this saves someone else some headaches

Using Python, Pandas, and mySQL and cannot get a stored procedure to return results at all, let alone into a data frame.

I keep receiving errors about multiple queries, but the stored procedures I am running are extremely simple parameter driven queries.

It doesn't matter what stored procedure I use, it is always the same result

In fact, the test procedure below (sp_test) is the following query -

select * from users;

If I run the same statement with

df=pd.read_sql("select * from users", cnx,index_col=None, coerce_float=True)

Instead of

df=pd.read_sql("call sp_test()", cnx,index_col=None, coerce_float=True)

It works fine, even though sp_test is select * from users

Why do I keep getting multi=true error messages and how do I go about fixing the problem and getting the results of my stored procedure? I don't understand how a simple select statement would return multiple result sets.

If there is another way to do this, happy to try it.

Following is the simple code I am using

import pandas as pd
from pandas.io.data import DataReader
from pandas import DataFrame
import mysql.connectorcnx = mysql.connector.connect(user='jeff', password='password', database='testdatabase', host='xx.xxx.xxx.xx')
df=pd.read_sql("call sp_test()", cnx,index_col=None, coerce_float=True)

When I get to pd.read_sql, I get the following error message

InterfaceError                            Traceback (most recent call last)
C:\Users\User\AppData\Local\Continuum\Anaconda3\lib\site-    packages\mysql\connector\cursor.py in execute(self, operation, params, multi)506             try:
--> 507                 self._handle_result(self._connection.cmd_query(stmt))508             except errors.InterfaceError:C:\Users\User\AppData\Local\Continuum\Anaconda3\lib\site-packages\mysql\connector\connection.py in cmd_query(self, query)
725             raise errors.InterfaceError(
--> 726                 'Use cmd_query_iter for statements with multiple queries.')
727 InterfaceError: Use cmd_query_iter for statements with multiple queries.During handling of the above exception, another exception occurred:InterfaceError                            Traceback (most recent call last)
C:\Users\User\AppData\Local\Continuum\Anaconda3\lib\site-    packages\pandas\io\sql.py in execute(self, *args, **kwargs)1563             else:
-> 1564                 cur.execute(*args)1565             return curC:\Users\User\AppData\Local\Continuum\Anaconda3\lib\site-    packages\mysql\connector\cursor.py in execute(self, operation, params, multi)510                     raise errors.InterfaceError(
--> 511                         "Use multi=True when executing multiple statements")512                 raiseInterfaceError: Use multi=True when executing multiple statements
Answer

Hopefully this will be of some help. Using some concept pointers from here, and a little trial / error, I was able to make this work using mysql.connector and pandas.

# CONNECT TO DB AND GET CURSOR OBJECT
conn = <do db connecty stuff>
cur = conn.cursor()# CALL THE STORED PROCEDURE
cur.callproc('stored_proc_name', ['my', 'usp', 'parameters'])# EXTRACT RESULTS FROM CURSOR
for i in cur.stored_results(): results = i.fetchall()# LOAD INTO A DATAFRAME
df = pd.DataFrame(results, columns=['my', 'column', 'headers'])

This worked perfectly for me ... I hope it does for you too.

https://en.xdnf.cn/q/69332.html

Related Q&A

How can I change the font size in GTK?

Is there an easy way to change the font size of text elements in GTK? Right now the best I can do is do set_markup on a label, with something silly like:lbl.set_markup("<span font_desc=Tahoma …

How to read BigQuery table using python pipeline code in GCP Dataflow

Could someone please share syntax to read/write bigquery table in a pipeline written in python for GCP Dataflow

How can I wrap a python function in a way that works with with inspect.signature?

Some uncontroversial background experimentation up front: import inspectdef func(foo, bar):passprint(inspect.signature(func)) # Prints "(foo, bar)" like youd expectdef decorator(fn):def _wra…

Python OpenCV Error: TypeError: Image data cannot be converted to float

So I am trying to create a Python Program to detect similar details in two images using Pythons OpenCV. I have the two images and they are in my current directory, and they exist (see the code in line…

Specify timestamp on each packet in Scapy?

With Scapy, when I create a packet and write it to a pcap file, it sets the timestamp of the packet to the current time.This is my current usage. 1335494712.991895 being the time I created the packet:&…

Converting a dataframe to dictionary with multiple values

I have a dataframe likeSr.No ID A B C D1 Tom Earth English BMW2 Tom Mars Spanish BMW Green 3 Michael Mercury Hindi …

How do I create KeyPoints to compute SIFT?

I am using OpenCV-Python.I have identified corner points using cv2.cornerHarris. The output is of type dst.I need to compute SIFT features of the corner points. The input to sift.compute() has to be of…

Error in Tensorboards(PyTorch) add_graph

Im following this Pytorchs Tensorboard documentation. I have the following code: model = torchvision.models.resnet50(False) writer.add_graph(model)It throws the following error:_ = model(*args) # dont…

Population must be a sequence or set. For dicts, use list(d)

I try to excute this code and I get the error bellow, I get the error in the random function and I dont know how to fix it, please help me.def load_data(sample_split=0.3, usage=Training, to_cat=True, v…

Why do imports fail in setuptools entry_point scripts, but not in python interpreter?

I have the following project structure:project |-project.py |-__init__.py |-setup.py |-lib|-__init__.py|-project|-__init__.py|-tools.pywith project.py:from project.lib import *def main():print("ma…