- Python 資料訪問教程
- Python 資料訪問 - 首頁
- Python MySQL
- Python MySQL - 簡介
- Python MySQL - 資料庫連線
- Python MySQL - 建立資料庫
- Python MySQL - 建立表
- Python MySQL - 插入資料
- Python MySQL - 查詢資料
- Python MySQL - WHERE 子句
- Python MySQL - ORDER BY 子句
- Python MySQL - 更新表
- Python MySQL - 刪除資料
- Python MySQL - 刪除表
- Python MySQL - LIMIT 子句
- Python MySQL - JOIN 子句
- Python MySQL - 遊標物件
- Python PostgreSQL
- Python PostgreSQL - 簡介
- Python PostgreSQL - 資料庫連線
- Python PostgreSQL - 建立資料庫
- Python PostgreSQL - 建立表
- Python PostgreSQL - 插入資料
- Python PostgreSQL - 查詢資料
- Python PostgreSQL - WHERE 子句
- Python PostgreSQL - ORDER BY 子句
- Python PostgreSQL - 更新表
- Python PostgreSQL - 刪除資料
- Python PostgreSQL - 刪除表
- Python PostgreSQL - LIMIT 子句
- Python PostgreSQL - JOIN 子句
- Python PostgreSQL - 遊標物件
- Python SQLite
- Python SQLite - 簡介
- Python SQLite - 建立連線
- Python SQLite - 建立表
- Python SQLite - 插入資料
- Python SQLite - 查詢資料
- Python SQLite - WHERE 子句
- Python SQLite - ORDER BY 子句
- Python SQLite - 更新表
- Python SQLite - 刪除資料
- Python SQLite - 刪除表
- Python SQLite - LIMIT 子句
- Python SQLite - JOIN 子句
- Python SQLite - 遊標物件
- Python MongoDB
- Python MongoDB - 簡介
- Python MongoDB - 建立資料庫
- Python MongoDB - 建立集合
- Python MongoDB - 插入文件
- Python MongoDB - 查詢
- Python MongoDB - 查詢
- Python MongoDB - 排序
- Python MongoDB - 刪除文件
- Python MongoDB - 刪除集合
- Python MongoDB - 更新
- Python MongoDB - LIMIT 子句
- Python 資料訪問資源
- Python 資料訪問 - 快速指南
- Python 資料訪問 - 有用資源
- Python 資料訪問 - 討論
Python SQLite - 刪除表
您可以使用 DROP TABLE 語句刪除整個表。您只需要指定要刪除的表的名稱。
語法
以下是 PostgreSQL 中 DROP TABLE 語句的語法:
DROP TABLE table_name;
示例
假設我們使用以下查詢建立了名為 CRICKETERS 和 EMPLOYEES 的兩個表:
sqlite> CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); sqlite> CREATE TABLE EMPLOYEE( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT ); sqlite>
現在,如果您使用.tables命令驗證表列表,您可以在其中看到上面建立的表(列表)如下:
sqlite> .tables CRICKETERS EMPLOYEE sqlite>
以下語句將從資料庫中刪除名為 Employee 的表:
sqlite> DROP table employee; sqlite>
由於您已刪除 Employee 表,如果您再次檢索表列表,您將只能看到其中一個表。
sqlite> .tables CRICKETERS sqlite>
如果您嘗試再次刪除 Employee 表,由於您已將其刪除,您將收到一條錯誤訊息,提示“no such table”,如下所示:
sqlite> DROP table employee; Error: no such table: employee sqlite>
要解決此問題,您可以將 IF EXISTS 子句與 DELTE 語句一起使用。如果表存在,則此語句將刪除該表;否則,將跳過 DLETE 操作。
sqlite> DROP table IF EXISTS employee; sqlite>
使用 Python 刪除表
您可以隨時使用 MYSQL 的 DROP 語句刪除表,但在刪除任何現有表時需要非常小心,因為刪除表後將無法恢復丟失的資料。
示例
要使用 python 從 SQLite3 資料庫中刪除表,請在遊標物件上呼叫execute()方法,並將 drop 語句作為引數傳遞給它。
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
#Doping EMPLOYEE table if already exists
cursor.execute("DROP TABLE emp")
print("Table dropped... ")
#Commit your changes in the database
conn.commit()
#Closing the connection
conn.close()
輸出
Table dropped...
廣告