Pandas IO SQL和具有多个结果集的存储过程

时间:2016-04-14 15:04:24

标签: python sql-server pandas stored-procedures pandasql

所以我在本地sql server上有一个存储过程,这会返回多个数据集/表

通常,在python / pyodbc中我会使用

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

我希望使用ps.io.sql.read_sql并将带有多个结果集的存储过程返回到数据帧中,但是我无法找到任何引用如何移动光标并在关闭之前获取更多信息的内容关闭。

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()

1 个答案:

答案 0 :(得分:2)

以下内容应该有效:

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)

编辑:我在此处更新了代码,以避免必须手动指定列名。

请注意,原始问题仅指定“本地SQL服务器”,而不是特定类型的SQL服务器。这个答案适用于MySQL,但我还没有测试过任何其他类型。