Python 数据访问教程

Python 数据访问主页

Python MySQL

Python MySQL 简介 Python MySQL 数据库连接 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入数据 Python MySQL 选择数据 Python MySQL Where 子句 Python MySQL 排序 Python MySQL 更新表 Python MySQL 删除数据 Python MySQL 删除表 Python MySQL Limit 子句 Python MySQL 连接 Python MySQL 游标对象

Python PostgreSQL

Python PostgreSQL 简介 Python PostgreSQL 数据库连接 Python PostgreSQL 创建数据库 Python PostgreSQL 创建表 Python PostgreSQL 插入数据 Python PostgreSQL 选择数据 Python PostgreSQL Where 子句 Python PostgreSQL 排序 Python PostgreSQL 更新表 Python PostgreSQL 删除数据 Python PostgreSQL 删除表 Python PostgreSQL Limit 子句 Python PostgreSQL 连接 Python PostgreSQL 游标对象

Python SQLite

Python SQLite 简介 Python SQLite 建立连接 Python SQLite 创建表 Python SQLite 插入数据 Python SQLite 选择数据 Python SQLite Where 子句 Python SQLite 排序 Python SQLite 更新表 Python SQLite 删除数据 Python SQLite 删除表 Python SQLite Limit 子句 Python SQLite 连接 Python SQLite 游标对象

Python MongoDB

Python MongoDB 简介 Python MongoDB 创建数据库 Python MongoDB 创建集合 Python MongoDB 插入文档 Python MongoDB 查找 Python MongoDB 查询 Python MongoDB 排序 Python MongoDB 删除文档 Python MongoDB 删除集合 Python MongoDB 更新 Python MongoDB Limit 子句

Python 数据访问资源

Python 数据访问 快速指南 Python 数据访问 有用资源 Python 数据访问 讨论


Python MySQL - 删除表

您可以使用 DROP TABLE 语句删除整个表。您只需指定需要删除的表的名称即可。

语法

以下是 MySQL 中 DROP TABLE 语句的语法 −

DROP TABLE table_name;

示例

在删除表之前,使用 SHOW TABLES 语句获取表列表,如下所示 −

mysql> SHOW TABLES;
+-----------------+
| Tables_in_mydb  |
+-----------------+
| contact         |
| cricketers_data |
| employee        |
| sample          |
| tutorials       |
+-----------------+
5 rows in set (0.00 sec)

以下语句将从数据库中完全删除名为 sample 的表 −

mysql> DROP TABLE sample;
Query OK, 0 rows affected (0.29 sec)

由于我们已经从 MySQL 中删除了名为 sample 的表,如果您再次获取表列表,您将找不到其中的表名 sample。

mysql> SHOW TABLES;
+-----------------+
| Tables_in_mydb  |
+-----------------+
| contact         |
| cricketers_data |
| employee        |
| tutorials       |
+-----------------+
4 rows in set (0.00 sec)

使用 python 删除表

您可以使用 MYSQL 的 DROP 语句随时删除表,但在删除任何现有表时需要非常小心,因为删除表后丢失的数据将无法恢复。

要使用 python 从 MYSQL 数据库中删除表,请在游标对象上调用 execute() 方法,并将 drop 语句作为参数传递给它。

示例

下表从数据库中删除了一个名为 EMPLOYEE 的表。

import mysql.connector

#建立连接 conn = mysql.connector.connect(
   user='root', password='password', host='127.0.0.1', database='mydb'
)

#使用 cursor() 方法创建游标对象 
cursor = conn.cursor()

#检索表格列表打印("List of tables in the database: ") 
   cursor.execute("SHOW Tables") print(cursor.fetchall())

#如果 EMPLOYEE 表已经存在,则执行 cursor.execute
   ("DROP TABLE EMPLOYEE") print("Table dropped... ")

#Retrieving the list of tables print(
   "List of tables after dropping the EMPLOYEE table: ") 
   cursor.execute("SHOW Tables") print(cursor.fetchall())

#关闭连接 conn.close()

输出

List of tables in the database:
[('employee',), ('employeedata',), ('sample',), ('tutorials',)]
Table dropped...
List of tables after dropping the EMPLOYEE table:
[('employeedata',), ('sample',), ('tutorials',)]

仅当表存在时才删除

如果您尝试删除数据库中不存在的表,则会发生错误,如下所示−

mysql.connector.errors.ProgrammingError: 1051 (42S02): 
   Unknown table 'mydb.employee'

您可以通过在 DELETE 语句中添加 IF EXISTS 来验证表在删除之前是否存在,从而避免出现此错误。

import mysql.connector

#建立连接
conn = mysql.connector.connect(
   user='root', password='password', host='127.0.0.1', database='mydb')

#使用 cursor() 方法创建游标对象
cursor = conn.cursor()

#检索表列表
print("数据库中的表列表: ")
cursor.execute("SHOW Tables")
print(cursor.fetchall())

#如果 EMPLOYEE 表已存在,则将其删除
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
print("Table dropped... ")

#检索表列表
print("List of tables after dropping the EMPLOYEE table: ")
cursor.execute("SHOW Tables")
print(cursor.fetchall())

#关闭连接
conn.close()

输出

List of tables in the database:
[('employeedata',), ('sample',), ('tutorials',)]
Table dropped...
List of tables after dropping the EMPLOYEE table:
[('employeedata',), ('sample',),
('tutorials',)]