Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use multiple parameters using pandas pd.read_sql_query?

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.

Here is my code that works.

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

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

output.

     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: []
like image 438
mattk Avatar asked Oct 21 '16 15:10

mattk


1 Answers

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))
like image 114
Steven G Avatar answered Nov 16 '22 05:11

Steven G