cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
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
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

@Kaniz 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.