Python MySQL 删除表用法指南

2021年3月11日16:57:11 发表评论 635 次浏览

当我们必须将MySQL与其他编程语言一起使用时, 可以使用连接器。 MySQL-connector的工作是提供对使用所需语言的MySQL驱动程序的访问。因此, 它在编程语言和MySQL Server之间生成了连接。

删除表命令

Drop命令影响表的结构而不影响数据。它用于删除已经存在的表。如果你不确定要删除的表是否存在如果存在则删除表使用命令。在以下示例中将处理这两种情况。

语法如下:

DROP TABLE tablename;

DROP TABLE IF EXISTS tablename;

以下程序将帮助你更好地理解这一点。

下降前的表:

python-mysql-drop

范例1:演示删除的程序(如果存在)。我们将尝试删除上述数据库中不存在的表。

# Python program to demonstrate
# drop clause
  
  
import mysql.connector
  
# Connecting to the Database
mydb = mysql.connector.connect(
   host = 'localhost' , database = 'College' , user = 'root' , )
  
cs = mydb.cursor()
  
# drop clause
statement = "Drop Table if exists Employee"
  
# Uncommenting statement ="DROP TABLE employee"
# Will raise an error as the table employee
# does not exists
  
cs.execute(statement)
      
# Disconnecting from the database
mydb.close()

输出如下:

python-mysql-drop-1

范例2:程序放表极客

# Python program to demonstrate
# drop clause
  
  
import mysql.connector
  
# Connecting to the Database
mydb = mysql.connector.connect(
   host = 'localhost' , database = 'College' , user = 'root' , )
  
cs = mydb.cursor()
  
# drop clause
statement = "DROP TABLE Geeks"
  
cs.execute(statement)
      
# Disconnecting from the database
mydb.close()

输出如下:

python-mysql-drop-2

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

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