如何使用pandas pd.read_sql_query使用多个参数? [英] How can I use multiple parameters using pandas pd.read_sql_query?

查看:1493
本文介绍了如何使用pandas pd.read_sql_query使用多个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在sql查询中传递三个变量.这些是区域,功能,newUser.我正在使用SQL驱动程序SQL Server Native Client 11.0.

I am trying to pass three variables in a sql query. These are region, feature, newUser. I am using SQL driver 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

相反,我想传递三个变量,而此代码不起作用.

Instead i want to pass in three variables and this code does not work.

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

输出返回一个空的数据帧.

Output returns an empty data frame.

Empty DataFrame
Columns: [LicenseNo]
Index: []

推荐答案

在元组中尝试字符串,也可以在查询中取出():

try a string in a tuple, also you can take out the () in the query:

所以您可以做类似的事情

so you could do something like

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

这篇关于如何使用pandas pd.read_sql_query使用多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆