sqlite 工具 Python(開發者使用指南)
SQLite-utils Python 套件是一個功能強大的工具,其中包含用於操作 SQLite 資料庫的 Python 實用函數。 它提供命令列介面 (CLI) 和 Python 庫,使建立、操作和查詢 SQLite 資料庫變得容易。 讓我們深入了解它的功能並看一些程式碼範例。 本文稍後將探討IronPDF ,這是一個由Iron Software開發的 PDF 生成庫。
SQLite-utils 概述
SQLite-utils旨在簡化與操作 SQLite 資料庫相關的各種任務。 它的一些主要特點包括:
*建立和管理資料庫:*輕鬆建立新的資料庫和表格。 插入和查詢資料:插入 JSON 資料、CSV 或 TSV 檔案並執行 SQL 查詢。 全文搜尋:配置和執行全文搜尋查詢。 模式轉換:執行 SQLite 的 ALTER TABLE 不支援的模式變更。 資料標準化:將列提取到單獨的表中以標準化資料。 自訂 SQL 函數:**安裝外掛程式以新增自訂 SQL 函數。
安裝
您可以使用 pip 安裝SQLite-utils :
pip install sqlite-utilspip install sqlite-utils或者,如果您在 macOS 上使用 Homebrew:
brew install sqlite-utilsbrew install sqlite-utils使用 SQLite-utils 作為 CLI 工具
CLI 工具可讓您直接從命令列執行各種操作。以下是一些範例:
建立資料庫並插入數據
讓我們建立一個新的 SQLite 資料庫,並從 CSV 檔案中插入一些資料:
# Create a new database and insert data from a CSV file
sqlite-utils insert dogs.db dogs dogs.csv --csv# Create a new database and insert data from a CSV file
sqlite-utils insert dogs.db dogs dogs.csv --csv查詢數據
以下命令示範如何從資料庫執行 SQL 查詢:
# Query the database and display results in JSON format
sqlite-utils dogs.db "select * from dogs" --json# Query the database and display results in JSON format
sqlite-utils dogs.db "select * from dogs" --json表格列表
列出資料庫中所有表格及其行數:
sqlite-utils tables dogs.db --countssqlite-utils tables dogs.db --counts使用 SQLite-utils 作為 Python 函式庫
您也可以使用 SQLite-utils 作為 Python 函式庫,以程式設計方式與 SQLite 資料庫進行互動。
建立資料庫並插入數據
以下是如何使用 Python 建立新資料庫並插入資料:
import sqlite_utils
# Create a new database
db = sqlite_utils.Database("demo_database.db")
# Insert data into a table
db["dogs"].insert_all([
{"id": 1, "age": 4, "name": "Cleo"},
{"id": 2, "age": 2, "name": "Pancakes"}
], pk="id")import sqlite_utils
# Create a new database
db = sqlite_utils.Database("demo_database.db")
# Insert data into a table
db["dogs"].insert_all([
{"id": 1, "age": 4, "name": "Cleo"},
{"id": 2, "age": 2, "name": "Pancakes"}
], pk="id")查詢數據
您可以執行 SQL 查詢並取得結果:
# Run a query and fetch results
rows = db.query("SELECT * FROM dogs")
for row in rows:
print(row)# Run a query and fetch results
rows = db.query("SELECT * FROM dogs")
for row in rows:
print(row)全文檢索
啟用表格全文搜尋並執行搜尋查詢:
# Enable full-text search on the 'name' column
db["dogs"].enable_fts(["name"])
# Run a search query for the term "Cleo"
results = db["dogs"].search("Cleo")
for result in results:
print(result)# Enable full-text search on the 'name' column
db["dogs"].enable_fts(["name"])
# Run a search query for the term "Cleo"
results = db["dogs"].search("Cleo")
for result in results:
print(result)IronPDF簡介
! sqlite 工具 Python(開發者使用指南):圖 1 - IronPDF:Python PDF 函式庫
IronPDF是一個功能強大的 Python 庫,旨在利用 HTML、CSS、圖像和 JavaScript 創建、編輯和簽署 PDF 文件。 它提供商業級的效能,同時佔用記憶體極少。 主要特點包括:
HTML 轉 PDF:
將 HTML 檔案、HTML 字串和 URL 轉換為 PDF。 例如,使用 Chrome PDF 渲染器將網頁渲染為 PDF。
跨平台支援:
相容於各種 .NET 平台,包括 .NET Core、.NET Standard 和 .NET Framework。 它支援 Windows、Linux 和 macOS。
編輯和簽署:
設定屬性,使用密碼和權限添加安全性,並為您的 PDF 應用數位簽章。
頁面範本和設定:
您可以自訂 PDF 文件,包括新增頁首、頁尾、頁碼和可調整的頁邊距。 它還支援自訂紙張尺寸和響應式佈局。
標準符合性:
符合 PDF 標準,包括 PDF/A 和 PDF/UA,支援 UTF-8 字元編碼,並管理圖像、CSS 和字體等資源。
使用 IronPDF 和 Sqlite Utils 產生 PDF 文檔
import sqlite_utils
from ironpdf import ChromePdfRenderer, License
# Apply your license key
License.LicenseKey = "key"
# Initialize a new database
db = sqlite_utils.Database("mydatabase.db")
# Define a table schema
schema = {
"id": int,
"name": str,
"age": int
}
# Create a table with the defined schema
db["users"].create(schema)
# Sample data to insert into the table
data = [
{"id": 1, "name": "Alice", "age": 30},
{"id": 2, "name": "Bob", "age": 28},
{"id": 3, "name": "Charlie", "age": 32}
]
# Insert data into the table
db["users"].insert_all(data)
# Query all records from the table
rows = db.query("SELECT * FROM users")
# Initialize the PDF renderer
renderer = ChromePdfRenderer()
# Create HTML content for the PDF
content = "<h1>Awesome IronPDF with Sqlite-Utils</h1>"
content += "<p>Table data:</p>"
for row in rows:
content += f"<p>{row}</p>"
# Render the HTML content as PDF
pdf = renderer.RenderHtmlAsPdf(content)
# Save the generated PDF as a file
pdf.save_as("DemoSqliteUtils.pdf")import sqlite_utils
from ironpdf import ChromePdfRenderer, License
# Apply your license key
License.LicenseKey = "key"
# Initialize a new database
db = sqlite_utils.Database("mydatabase.db")
# Define a table schema
schema = {
"id": int,
"name": str,
"age": int
}
# Create a table with the defined schema
db["users"].create(schema)
# Sample data to insert into the table
data = [
{"id": 1, "name": "Alice", "age": 30},
{"id": 2, "name": "Bob", "age": 28},
{"id": 3, "name": "Charlie", "age": 32}
]
# Insert data into the table
db["users"].insert_all(data)
# Query all records from the table
rows = db.query("SELECT * FROM users")
# Initialize the PDF renderer
renderer = ChromePdfRenderer()
# Create HTML content for the PDF
content = "<h1>Awesome IronPDF with Sqlite-Utils</h1>"
content += "<p>Table data:</p>"
for row in rows:
content += f"<p>{row}</p>"
# Render the HTML content as PDF
pdf = renderer.RenderHtmlAsPdf(content)
# Save the generated PDF as a file
pdf.save_as("DemoSqliteUtils.pdf")程式碼解釋
該腳本結合了 SQLite-utils Python 套件和 IronPDF 庫的功能,用於管理 SQLite 資料庫並產生 PDF 文件。 以下是這段程式碼功能的逐步解釋:
1.資料庫初始化:
- 使用 SQLite-utils 初始化名為"mydatabase.db"的 SQLite 資料庫。
2.建立表格:
- 定義一個包含
id、name和age列的表架構。 - 使用定義的模式在 SQLite 資料庫中建立一個名為"users"的表。
3.資料插入:
- 使用 SQLite-utils 向"users"表中插入多筆記錄。
4.資料查詢:
- 從"users"表中檢索所有記錄,並建立資料的HTML表示形式。
- PDF 生成:
- 使用 IronPDF 建立 PDF 文件。
- 建立 PDF 文件的 HTML 內容,包括從 SQLite 資料庫檢索的標題和表格資料。
- 將產生的 PDF 文件儲存為"DemoSqliteUtils.pdf"。
總的來說,腳本示範如何利用 SQLite-utils 執行資料庫管理任務,例如建立表格、插入資料和查詢,並結合 IronPDF 從 Python 應用程式中 SQLite 資料庫中的動態內容來產生 PDF 文件。
輸出
IronPDF 許可
IronPDF使用 Python 許可證金鑰運作。 IronPDF for Python 提供免費試用許可證金鑰,使用戶能夠在購買前測試其豐富的功能。
在使用 IronPDF 軟體包之前,請將許可證密鑰放在腳本的開頭:
from ironpdf import License
# Apply your license key
License.LicenseKey = "key"from ironpdf import License
# Apply your license key
License.LicenseKey = "key"結論
SQLite-utils 是一個功能強大的 SQLite 資料庫操作工具。 它同時提供命令列介面和 Python 庫。 無論您是需要從命令列快速操作數據,還是需要將 SQLite 操作整合到您的 Python 應用程式中,SQLite 都提供了一個靈活且易於使用的解決方案。







