gpt4 book ai didi

python - 如何使用 pandas pd.read_sql_query 使用多个参数?

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:26 27 4
gpt4 key购买 nike

我试图在 sql 查询中传递三个变量。这些是地区、功能、新用户。我正在使用 SQL 驱动程序 SQL Server Native Client 11.0。

这是我的有效代码。

query = "SELECT LicenseNo FROM License_Mgmt_Reporting.dbo.MATLAB_NNU_OPTIONS  WHERE Region = ?"

data_df = pd.read_sql_query((query),engine,params={region})

输出。

     LicenseNo
0 12
1 5

相反,我想传入三个变量,但这段代码不起作用。

query = "SELECT LicenseNo FROM License_Mgmt_Reporting.dbo.MATLAB_NNU_OPTIONS WHERE Region = ? and FeatureName = ? and NewUser =?"

nnu_data_df = pd.read_sql_query((query),engine,params={region, feature, newUser})

输出返回一个空数据框。

Empty DataFrame
Columns: [LicenseNo]
Index: []

最佳答案

尝试一个元组中的字符串,也可以去掉查询中的():

所以你可以做类似的事情

query = "SELECT LicenseNo FROM License_Mgmt_Reporting.dbo.MATLAB_NNU_OPTIONS WHERE Region = ? and FeatureName = ? and NewUser =?"
region = 'US'
feature = 'tall'
newUser = 'john'
data_df = pd.read_sql_query(query, engine, params=(region, feature , newUser))

关于python - 如何使用 pandas pd.read_sql_query 使用多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40180884/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com