返回sql查询执行的最后一个数据集

2024-06-01 13:11:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究从sql查询保存数据。你知道吗

我的代码运行良好,但它只返回查询的第一个结果数据集。你知道吗

例如,如果我有以下查询:

SELECT * FROM Table1
GO

SELECT * from Table2

或者

SELECT * FROM Table1
SELECT * FROM Table2

我只需要返回表2中的数据。你知道吗

数据库:Microsoft SQL Server 2017

datasets = []

with open('protected_script.sql', 'r') as protected_script:
    cursor = connection.cursor()
    cursor.execute(protected_script.read())
    datasets.append(cursor.fetchall())

Tags: 数据from数据库gosqlscriptselectcursor