Python MySQL 删除表用法指南

当我们必须将MySQL与其他编程语言一起使用时, 可以使用连接器。 MySQL-connector的工作是提供对使用所需语言的MySQL驱动程序的访问。因此, 它在编程语言和MySQL Server之间生成了连接。
删除表命令 Drop命令影响表的结构而不影响数据。它用于删除已经存在的表。如果你不确定要删除的表是否存在如果存在则删除表使用命令。在以下示例中将处理这两种情况。
语法如下:

DROP TABLE tablename; DROP TABLE IF EXISTS tablename;

以下程序将帮助你更好地理解这一点。
下降前的表:
Python MySQL 删除表用法指南

文章图片
【Python MySQL 删除表用法指南】范例1:演示删除的程序(如果存在)。我们将尝试删除上述数据库中不存在的表。
# Python program to demonstrate # drop clauseimport 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 existscs.execute(statement)# Disconnecting from the database mydb.close()

输出如下:
Python MySQL 删除表用法指南

文章图片
范例2:程序放表极客
# Python program to demonstrate # drop clauseimport 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 删除表用法指南

文章图片
注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

    推荐阅读