Python 和 MySQL - 刪除資料庫示例



Python 使用 c.execute(q) 函式建立或刪除 MySQL 資料庫,其中 c 為遊標,q 為要執行的查詢。

語法

# execute SQL query using execute() method.
cursor.execute(sql)

序號。 引數和說明
1

$sql

必需 - 用於刪除 MySQL 資料庫的 SQL 查詢。

示例

嘗試以下示例以刪除資料庫 −

將以下示例複製並貼上為 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()

輸出

使用 Python 執行 mysql_example.py 指令碼,並驗證輸出。

Database dropped
廣告
© . All rights reserved.