赛捷软件论坛's Archiver

vicky.yu 发表于 2021-6-7 09:33

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)
运行实例
MySQL Delete
MySQL Update


Python 参考手册
Python 实例
Python 测验


W3School 简体中文版提供的内容仅用于培训和测试,不保证内容的正确性。通过使用本站内容随之而来的风险与本站无关。版权所有,保留一

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.