Python MySql - Drop Tables



Python uses c.execute(q) function to drop a table where c is cursor and q is the delete query to be executed.

Syntax

# execute SQL query using execute() method.
cursor.execute(sql)
Sr.No. Parameter & Description
1

$sql

Required - SQL query to drop a table.

Example - Dropping Table

Try the following example to drop a table −

Copy and paste the following example as mysql_example.ty −

import mysql.connector

# Open database connection
db = mysql.connector.connect(host="localhost",user="root",password="root@123", database="TUTORIALS")

# prepare a cursor object using cursor() method
cursor = db.cursor()

# execute SQL query using execute() method.
cursor.execute("Drop Table tutorials_tbl")

print('Table dropped');

# disconnect from server
db.close()

Output

Execute the main.py script using python and verify the output.

Table dropped
Advertisements