cancel
Showing results for 
Search instead for 
Did you mean: 
Community Discussions
cancel
Showing results for 
Search instead for 
Did you mean: 

pass a tuple as parameter to sql query

kll
New Contributor III
at_lst = ['131','132','133']

at_tup = (*at_lst,)
 
print(at_tup)
('131','132','133')<div> <div><span>In my sql query, i am trying to pass this on a parameter, however, it doesn't work. <div> <div><div><div><span>%sql<br /><div><span>select * from main.schema.tbl<div><span>where code IN <span>( '{{ at_tup }}' )<div> <div><span>^ This return no result. <div> <div><span>However, when I pass the actual tuple, i get the desired results: <div> <div><div><span>select * from main.schema.tbl<div><span>where code IN ('131','132','133')<div> <div>How do I pass a tuple as a paramter the sql query?</div></div></span></div></span></div></div></div></span></div></div></span></div></div></span></span></div></span></div></span></div></div></div></div></span></div></div>
2 REPLIES 2

Kaniz
Community Manager
Community Manager

Hi @kllTo pass a tuple as a parameter to an SQL query, you can make use of the execute method of the cursor object in Python. The execute method allows you to prepare and run a database query or command, and it accepts a dictionary as a parameter where you can pass your tuple values.

kll
New Contributor III

@Kaniz  I am writing sql using the magic command in the cell block, `%%sql`. Is there a way to pass a parameter in the query without using the `execute` method of the cursor object? Can you please share an example? 

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.