Skip to content Skip to sidebar Skip to footer

Drop Table In Sql Database From Azure Databricks

I need to drop a table in SQL Database using Azure Databricks (PySpark). I am required to not use anything outside PySpark to achieve this. Can this be done to using DataFrameWrite

Solution 1:

df = spark.read.format('jdbc')\
.option("url", "jdbc:sqlserver://MyServerName:MyPort")\
.option("database", 'MyDBName')\
.option("user", "MyUser")\
.option("password", "MyPassword")\
.option('query', 'drop table tablename')

this did the trick.

Solution 2:

you can try the following.

#Setup the JDBC connection. 
jdbcUrl = "jdbc:mysql://{0}:{1}/{2}".format(jdbcHostname, jdbcPort, jdbcDatabase)
connectionProperties = {
      "user" : jdbcUsername,
      "password" : jdbcPassword,
      "driver" : "com.mysql.jdbc.Driver"
    }

#Create a query to drop the required table
pushdown_query = "DROP table tablename"#run the query
df = spark.read.jdbc(url=jdbcUrl, table=pushdown_query, properties=connectionProperties)
display(df)

I hope this helps.

Post a Comment for "Drop Table In Sql Database From Azure Databricks"