Python & MySQL - Drop Database Example



Python uses c.execute(q) function to create or delete a MySQL database where c is cursor and q is the 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 MySQL database.

Example

Try the following example to drop a database −

Copy and paste the following example as mysql_example.ty −

#!/usr/bin/python

import MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","root","root@123")

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

# execute SQL query using execute() method.
cursor.execute("DROP DATABASE TUTORIALS")

print('Database dropped');

# disconnect from server
db.close()

Output

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

Database dropped
Advertisements