Python / 如何在建立 SQLite 資料庫並在 python 中執行 SQL Script

Python / 如何在建立 SQLite 資料庫並在 python 中執行 SQL Script

SQLite 雖然是一個資料庫,但它並非是一個完整的資料庫伺服器,它是以 C 語言為基礎並根據 SQL 標準開發出來的小型資料庫檔案格式,是一個開放原始碼的語言。SQLite 現在幾乎已經內建在許多跨平台的裝置上,而 sqlite3 是 Python 內建的套件,可以用以存取、執行 SQLite資料庫檔案。 ▍安裝 DB Browser ▍在 Python 上連線 SQLite 資料庫並執行 SQL Script sqlite3 是 Python 的內建套件,可以直接 import 取用。使用時先以 connect() 建立資料庫連線,並建立 cursor() 物件以呼叫 execute() 執行 SQL Script。最終記得 commit() 儲存上面執行的任何修改,並以 close() 關閉資料庫連線。 ▍讀取 SELECT 結果 使用 execute() 執行 SELECT 語法會以 tuple 串列的格式回傳資料 在官方的文件中的範例: ▍建立 temp table...

SQLite / 語法筆記

SQLite / 語法筆記

目錄 如何在 SQLite 實現 Truncate Table SQLite 的時間與日期函數 取得當下時間(在地時區) 建立 temp table ▍如何在 SQLite 實現 Truncate Table TRUNCATE 指令在 SQL Server 中是清除整張 table 的所有 records,但不會刪除整張表單也不會移除 table schema。SQLite 不是使用 TRUNCATE,而是使用 DELETE 這個指令。 Reference SQLite: TRUNCATE TABLE Statement | totn SQLite ▍SQLite 的時間與日期函數 在 SQLite 中有五個關於時間和日期的函數: date(time-value, ...) : The date() function returns the date in this...