cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Databricks SDK for Python: Errors with parameters for Statement Execution

Phuonganh
New Contributor II

Hi team,

Im using Databricks SDK for python to run SQL queries. I created a variable as below:

param = [{'name' : 'a', 'value' :x'}, {'name' : 'b', 'value' : 'y'}]

and passed it the statement as below

_ = w.statement_execution.execute_statement( warehouse_id = "123", catalog = "aaa", statement = "SELECT * FROM table_a WHERE name = :a and age = :b", parameters = param)

and I get an attribute error: 'dict' object has no attribute 'as_dict'

But this is exactly how databricks instructed on their website. Thank you for your help

 

2 REPLIES 2

Kaniz_Fatma
Community Manager
Community Manager

Hi @PhuonganhThis error is not directly related to the Databricks SDK, but rather a misunderstanding of how to pass parameters in your SQL query.

  • The param dictionary youโ€™ve defined seems to have a typo in the value for the โ€˜aโ€™ parameter. It should be a valid value (e.g., a string or numeric value). Make sure to replace 'x' with the actual value you intend to use.
  • Also, ensure that the keys (โ€˜aโ€™ and โ€˜bโ€™) match the placeholders in your SQL query (โ€œ:aโ€ and โ€œ:bโ€).
  • The Databricks SDK expects parameters to be passed as a list of dictionaries, where each dictionary contains the โ€˜nameโ€™ and โ€˜valueโ€™ keys.
  • Make sure that the parameters argument in your execute_statement call is correctly formatted. It should be a list of dictionaries, just like your param variable.

    Hopefully, this helps resolve the issue! ๐Ÿ˜Š

DonkeyKong
New Contributor II

@Kaniz_Fatma This does not help resolve the issue. I am experiencing the same issue when following the above pointers. Here is the statement:

response = w.statement_execution.execute_statement(
    statement='ALTER TABLE users ALTER COLUMN :col_name SET NOT NULL', 
    warehouse_id='<warehouseID>',
    parameters=[{'name': 'col_name', 'value': 'user', 'type': 'STRING'}]
)

This results in the same AttributeError outlined by @Phuonganh above.

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!