There are several methods for utilizing a Python variable in an SQL query in Databricks:
variable = "some_value"
query = f"SELECT * FROM table WHERE column = '{variable}'"
spark.sql(query)
from pyspark.sql.functions import expr
variable = "some_value"
query = "SELECT * FROM table WHERE column = ?"
spark.sql(query).bind([expr(variable)]).show()
from pyspark.sql.functions import lit
variable = "some_value"
spark.range(1).withColumn("value", lit(variable)).createOrReplaceTempView("temp_table")
query = "SELECT * FROM table WHERE column = (SELECT value FROM temp_table)"
spark.sql(query)
Asked: 2023-06-13 14:11:07 +0000
Seen: 12 times
Last updated: Jun 13 '23