Python MySQL 删除表
Posted 吴吃辣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python MySQL 删除表相关的知识,希望对你有一定的参考价值。
章节
删除表
可以使用“DROP table”语句,删除现有的表:
示例
删除表“customers”:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="你的用户名",
passwd="你的密码",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "DROP TABLE customers"
mycursor.execute(sql)
仅当表存在时才删除
如果要删除的表不存在,会报错,可以使用If EXISTS关键字判断表是否存在,避免报错。
示例
删除存在的表“customers”:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="你的用户名",
passwd="你的密码",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "DROP TABLE IF EXISTS customers"
mycursor.execute(sql)
以上是关于Python MySQL 删除表的主要内容,如果未能解决你的问题,请参考以下文章
Python 2 和 Python 3 操作 MySQL 数据库实现创建表删除表增删改查操作
14.python与数据库之mysql:pymysqlsqlalchemy