7

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

2 Answers 2

8

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))
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Steven - I converted region, feature, and newUser to strings and used your syntax and still returned an empty data frame.
query = "SELECT LicenseNo FROM License_Mgmt_Reporting.dbo.MATLAB_NNU_OPTIONS WHERE Region = ? and FeatureName = ? and NewUser =?" Region = str(region) FeatureName = str(feature) NewUser = str(newUser) nnu_data_df = pd.read_sql_query(query,engine, params=(Region, FeatureName, NewUser))
sorry Steven I forgot to tag you
Can this work even with snowflake sql? even after setting snowflake.connector.paramstyle='qmark' its not working for me.
0

Operator error by me :( I was using the wrong variable and the database returned no results because it didn't exist!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.