六安做网站多少钱专业定制网页设计
Python语言基础——Python MySQL Drop Table
文章目录
- Python语言基础——Python MySQL Drop Table
 - 一、Python MySQL Drop Table
 
一、Python MySQL Drop Table
删除表
 您可以使用 “DROP TABLE” 语句来删除已有的表:
 实例
 删除 “customers” 表:
 import mysql.connector
 mydb = mysql.connector.connect(
 host=“localhost”,
 user=“yourusername”,
 passwd=“yourpassword”,
 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=“yourusername”,
 passwd=“yourpassword”,
 database=“mydatabase”
 )
 mycursor = mydb.cursor()
 sql = “DROP TABLE IF EXISTS customers”
 mycursor.execute(sql)
