Python MySQL Drop Table
Vee 'n tabel uit
Jy kan 'n bestaande tabel uitvee deur die "DROP TABLE"-stelling te gebruik:
Voorbeeld
Vee die tabel "kliënte" uit:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor =
mydb.cursor()
sql = "DROP TABLE customers"
mycursor.execute(sql)
Laat val slegs as dit bestaan
As die tabel wat jy wil uitvee reeds uitgevee is, of om enige ander rede nie bestaan nie, kan jy die IF EXISTS-sleutelwoord gebruik om te verhoed dat 'n fout word.
Voorbeeld
Vee die tabel "kliënte" uit as dit bestaan:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor =
mydb.cursor()
sql = "DROP TABLE IF EXISTS customers"
mycursor.execute(sql)