Python SQLite - 建立連線



要透過 SQLite 建立連線,請開啟命令提示符,瀏覽你安裝 SQLite 的位置,然後執行命令 sqlite3,如下所示 −

Command Prompt SQLite

使用 Python 建立連線

你可以使用 Python 模組 SQLite3 與 SQLite2 資料庫進行通訊。首先,你需要建立連線(建立一個連線物件)。

要使用 Python 與 SQLite3 資料庫建立連線,你需要 −

  • 使用 import 語句匯入 sqlite3 模組。

  • connect() 方法接受要連線的資料庫的名稱作為引數,並返回一個連線物件。

示例

import sqlite3
conn = sqlite3.connect('example.db')

輸出

print("Connection established ..........")
廣告