安裝 sqlite3

# 在 ubuntu linux 上安裝 sqlite3
apt-get install sqlite3

# 查看版本
sqlite3 --version

開啟一個名為 sample.db 的資料庫

sqlite3 sample.db

基本的操作

# 查看幫助文件
.help

# 查看目前的資料庫檔案
.database

# 顯示所有表格
.tables

# 建立一個 categories 資料表
CREATE TABLE categories(id INTEGER PRIMARY KEY, name text);

# 查看 categories 的結構
.schema categories

讀入準備好的 SQL 檔

create_articles_table.sql

CREATE TABLE articles (
  id INTEGER PRIMARY KEY,
  title TEXT,
  slug TEXT
);

.read create_articles_table.sql

調整輸出格式

# 調整顯示模式為 column
.mode column

# 在表頭顯示欄位名稱
.headers on

# 設定欄位寬度
.width 5 15 10

# 重置欄位寬度設定
.witdh 0

執行 SHELL 指令

.shell clear

index 操作

# 顯示 index
.indexes

# 建立 index
CREATE INDEX <INDEX_NAME> ON article(slug);

# 刪除 index
DROP INDEX <INDEX_NAME>

參考資料

https://geektutu.com/post/cheat-sheet-sqlite.html

Last modified: 2025-02-21

Author

Comments

Write a Reply or Comment

Your email address will not be published.