迭代 excel 并将数据作为字符串一个一个地提供给 pd.read_sql() 的任何优化方法

Any optimize way to iterate excel and provide data into pd.read_sql() as a string one by one

#here I have to apply the loop which can provide me the queries from excel for respective reports:
    df1 = pd.read_sql(SQLqueryB2, con=con1)
    df2 = pd.read_sql(ORCqueryC2, con=con2)
    if (df1.equals(df2)):
        print(Report2 +" : is Pass")

我们可以通过这样的方式实现上面的效果吗(通过迭代 ndarray) df = pd.read_excel(path) for col, item in df.iteritems(): 或者执行剩下的唯一选项,从“openpyxl”库中读取 excel 并迭代行、列,然后提供值。希望我问清楚了,如有疑问请评论我。

您正在尝试遍历 excel 文件,运行 2 个查询,查看它们是否匹配并输出结果,正确吗?

import pandas as pd
from sqlalchemy import create_engine

# add user, pass, database name
con = create_engine(f"mysql+pymysql://{USER}:{PWD}@{HOST}/{DB}")
file = pd.read_excel('excel_file.xlsx')

file['Result'] = ''  # placeholder
for i, row in file.iterrows():
    df1 = pd.read_sql(row['SQLQuery'], con)
    df2 = pd.read_sql(row['Oracle Queries'], con)
    file.loc[i, 'Result'] = 'Pass' if df1.equals(df2) else 'Fail'

file.to_excel('results.xlsx', index=False)

这将保存一个名为 results.xlsx 的文件,该文件反映了原始数据,但添加了一个名为 Result 的列,该列将是 Pass 或 Fail。

示例results.xlsx: