当前位置:Gxlcms > 数据库问题 > Python MySQL 删除表

Python MySQL 删除表

时间:2021-07-01 10:21:17 帮助过:3人阅读


章节

  • Python MySQL 入门
  • Python MySQL 创建数据库
  • Python MySQL 创建表
  • Python MySQL 插入表
  • Python MySQL Select
  • Python MySQL Where
  • Python MySQL Order By
  • Python MySQL Delete
  • Python MySQL 删除表
  • Python MySQL Update
  • Python MySQL Limit
  • Python MySQL Join

删除表

可以使用“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 删除表

标签:sele   mit   mys   tps   port   data   数据   nec   let   

人气教程排行