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

13,368

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))
Share:
13,368
mattk
Author by

mattk

Updated on June 17, 2022

Comments

  • mattk
    mattk almost 2 years

    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: []