从Databricks笔记本中删除Azure SQL表

huangapple go评论69阅读模式
英文:

Drop Azure SQL table from Databricks notebook

问题

我正在尝试通过我的 Databricks 笔记本在 Azure SQL Server 数据库中删除一个表(SQL 版本 2019)。

在我的笔记本中,我已经通过 JDBC 连接建立了与 Azure SQL Server 的连接。我有一个测试查询来确认连接。这个工作正常。

然而,我用于在 SQL 数据库中删除表的代码没有失败,但它也没有删除表。表仍然存在。

table_name = "dbo.json_staging"
spark.sql(f"DROP TABLE IF EXISTS {table_name}")

输出:Out[35]: DataFrame[]

英文:

I am trying to drop a table in my Azure SQL server database (SQL version 2019) via my Databricks notebook.

In my notebook, I have established by jdbc connection to the azure sql server. I have a test query to confirm the connection. This works fine.

However, my code to drop a table on the SQL database does not fail but it does not drop the table. The table still exists.

table_name = "dbo.json_staging"
spark.sql(f"DROP TABLE IF EXISTS {table_name}")

Output: Out[35]: DataFrame[]

答案1

得分: 1

我已经尝试使用以下代码在Azure Databricks中从Azure SQL数据库中删除表。

我使用了SCALA语言。

%scala
import java.util.Properties
import java.sql.DriverManager

val jdbcUsername = "admin02"
val jdbcPassword = "Welcome@1"
val driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
val jdbcUrl = s"jdbc:sqlserver://sqlserveraug09.database.windows.net:1433;database=db002;user=admin02@sqlserveraug09;password=Welcome@1;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"
val connectionProperties = new Properties()
connectionProperties.put("user", s"${jdbcUsername}")
connectionProperties.put("password", s"${jdbcPassword}")
connectionProperties.setProperty("Driver", driverClass)

val connection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword)
val stmt = connection.createStatement()
val sql = "DROP Table employee_new"
stmt.execute(sql)
connection.close()

输出:
从Databricks笔记本中删除Azure SQL表

我已经删除了名为employee_new的表。

从Databricks笔记本中删除Azure SQL表

英文:

I have tried the below code to DROP the table from the Azure SQL database Using Azure Databricks.

I have tried it using the SCALA

%scala
import  java.util.Properties
import  java.sql.DriverManager
val  jdbcUsername = "admin02"
val  jdbcPassword = "Welcome@1"
val  driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
val  jdbcUrl = s"jdbc:sqlserver://sqlserveraug09.database.windows.net:1433;database=db002;user=admin02@sqlserveraug09;password=Welcome@1;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"
val  connectionProperties = new  Properties()
connectionProperties.put("user", s"${jdbcUsername}")
connectionProperties.put("password", s"${jdbcPassword}")
connectionProperties.setProperty("Driver", driverClass)
val  connection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword)
val  stmt = connection.createStatement()
val  sql = "DROP Table employee_new"
stmt.execute(sql)
connection.close()

Output:
从Databricks笔记本中删除Azure SQL表

I have DROPED the Table named employee_new

从Databricks笔记本中删除Azure SQL表

huangapple
  • 本文由 发表于 2023年8月9日 07:20:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定