Python 與 MySQL -刪除表示例



Python 使用 c.execute(q) 函式刪除表,其中 c 是遊標,q 是要執行的刪除查詢。

語法

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

序號 引數與描述
1

$sql

必需 - 刪除表的 SQL 查詢。

示例

嘗試以下示例來刪除表 -

複製並貼上以下示例作為 mysql_example.ty -

#!/usr/bin/python

import MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","root","root@123","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()

程式碼

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

Table dropped
廣告
© . All rights reserved.