Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

python - Pandas IO SQL and stored procedure with multiple result sets

So I have a stored proc on a local sql server, this returns multiple data sets / tables

Normally, in python / pyodbc I would use

cursor.nextset()
subset1 = cursor.fetchall()
cursor.nextset()
subset2 = cursor.fetchall()

I wish to make use of the ps.io.sql.read_sql and return the stored procedure with multiple result sets into dataframes, however I can not find anything that references how to move the cursor along and get more information before closing things off.

import pandas as ps

query = "execute raw.GetDetails @someParam = '118'"
conn = myConnection() #connection,cursor

results = ps.io.sql.read_sql(query, con=conn[0])

results.head()

conn[1].close()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following should work:

import pandas as pd
from sqlalchemy import create_engine

engine = create_engine('mysql://{}:{}@{}/{}'.format(username, password, server, database_name))
connection = engine.connect().connection
cursor = self.connection.cursor()

cursor.execute('call storedProcName(%s, %s, ...)', params)

# Results set 1
column_names = [col[0] for col in cursor.description] # Get column names from MySQL

df1_data = []
for row in cursor.fetchall():
    df1_data.append({name: row[i] for i, name in enumerate(column_names)})

# Results set 2
cursor.nextset()
column_names = [col[0] for col in cursor.description] # Get column names from MySQL

df2_data = []
for row in cursor.fetchall():
    df2_data.append({name: row[j] for j, name in enumerate(column_names)})

cursor.close()

df1 = pd.DataFrame(df1_data)
df2 = pd.DataFrame(df2_data)

Edit: I've updated the code here to avoid having to manually specify the column names.

Note that the original question only specifies a "local SQL server", not a specific kind of SQL server. This answer works with MySQL, but I haven't tested it with any other variety.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...