Python MySQL 删除表

Python MySQL 教程

相关推荐

 

Python MySQL 删除表

2019年6月4日

 意见反馈

删除表

可以使用“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)

复制


Doc navigation

← Python MySQL Delete

Python MySQL Update →

猜你喜欢

转载自blog.csdn.net/matthewwu/article/details/93381126