Linux Ubuntu 查看 SQLite 工具
1. 命令行工具(最常用)
安装 SQLite3 命令行工具
bash
sudo apt update
sudo apt install sqlite3
基本使用
bash
# 打开数据库
sqlite3 ditu.sqlite
# 查看所有表
.tables
# 查看表结构
.schema positions
.schema groups
# 查询数据
SELECT * FROM positions;
SELECT * FROM groups;
# 格式化输出
.headers on
.mode column
SELECT * FROM positions;
# 退出
.exit
# 或按 Ctrl+D
常用命令
sql
-- 查看所有表
.tables
-- 查看表结构
.schema 表名
-- 查询数据
SELECT * FROM positions;
-- 按条件查询
SELECT * FROM positions WHERE group_name = '默认';
-- 统计数量
SELECT COUNT(*) FROM positions;
-- 分组统计
SELECT group_name, COUNT(*) FROM positions GROUP BY group_name;
-- 导出数据
.output output.csv
.mode csv
SELECT * FROM positions;
.output stdout
2. 图形化工具
SQLiteBrowser(推荐)
bash
# 安装
sudo apt install sqlitebrowser
# 启动
sqlitebrowser ditu.sqlite
DBeaver(功能强大)
bash
# 安装(需要 Java)
sudo snap install dbeaver-ce
# 启动
dbeaver-ce
VS Code 扩展
- 安装扩展 "SQLite" by alexcvzz
- 打开数据库文件,右键选择 "Open Database"
- 可以查看和查询数据
3. 其他命令行工具
使用 Python 查看
bash
# 交互式查看
python3 -c "
import sqlite3
conn = sqlite3.connect('ditu.sqlite')
cursor = conn.cursor()
cursor.execute('SELECT * FROM positions')
for row in cursor.fetchall():
print(row)
conn.close()
"
创建查看脚本
创建 view_db.py:
python
import sqlite3
from datetime import datetime
def view_database():
conn = sqlite3.connect('ditu.sqlite')
cursor = conn.cursor()
# 查看表结构
print("=== 表结构 ===")
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = cursor.fetchall()
for table in tables:
print(f"\n表: {table[0]}")
cursor.execute(f"PRAGMA table_info({table[0]})")
for col in cursor.fetchall():
print(f" {col[1]} ({col[2]})")
# 查看数据
print("\n=== 位置数据 ===")
cursor.execute("SELECT id, title, pos, group_name, abs, create_time FROM positions LIMIT 20")
for row in cursor.fetchall():
print(f"ID:{row[0]}, 标题:{row[1]}, 位置:{row[2]}, 分组:{row[3]}")
# 统计
cursor.execute("SELECT COUNT(*) FROM positions")
total = cursor.fetchone()[0]
print(f"\n总记录数: {total}")
cursor.execute("SELECT group_name, COUNT(*) FROM positions GROUP BY group_name")
print("\n分组统计:")
for row in cursor.fetchall():
print(f" {row[0]}: {row[1]}条")
conn.close()
if __name__ == "__main__":
view_database()
运行:
bash
python3 view_db.py
4. 快速查询命令
查看所有数据(JSON格式)
bash
sqlite3 ditu.sqlite "SELECT json_group_array(json_object('id',id,'title',title,'pos',pos,'group',group_name)) FROM positions;"
导出为 CSV
bash
sqlite3 -csv -header ditu.sqlite "SELECT * FROM positions;" > positions.csv
导出为 JSON
bash
sqlite3 -json ditu.sqlite "SELECT * FROM positions;" > positions.json
查看最近的记录
bash
sqlite3 ditu.sqlite "SELECT id, title, pos, datetime(create_time,'localtime') as time FROM positions ORDER BY id DESC LIMIT 10;"
5. 使用 SQLite 的 Web 工具
安装 phpLiteAdmin(Web界面)
bash
# 下载
wget https://phpliteadmin.org/downloads/phpliteadmin.zip
unzip phpliteadmin.zip
sudo mv phpliteadmin.php /var/www/html/
# 访问 http://localhost/phpliteadmin.php
6. 常用查询示例
bash
# 查看所有分组
sqlite3 ditu.sqlite "SELECT DISTINCT group_name FROM positions;"
# 查看某个分组的所有位置
sqlite3 ditu.sqlite "SELECT title, pos FROM positions WHERE group_name='默认';"
# 搜索包含特定关键词的位置
sqlite3 ditu.sqlite "SELECT * FROM positions WHERE title LIKE '%厦门%' OR abs LIKE '%厦门%';"
# 查看今天的记录
sqlite3 ditu.sqlite "SELECT * FROM positions WHERE date(create_time) = date('now','localtime');"
7. 创建快捷脚本
创建 db.sh:
bash
#!/bin/bash
# SQLite 数据库快捷操作
DB="ditu.sqlite"
case "$1" in
"list")
sqlite3 $DB "SELECT id, title, group_name, datetime(create_time,'localtime') FROM positions ORDER BY id DESC;"
;;
"groups")
sqlite3 $DB "SELECT group_name, COUNT(*) FROM positions GROUP BY group_name;"
;;
"search")
sqlite3 $DB "SELECT * FROM positions WHERE title LIKE '%$2%' OR abs LIKE '%$2%';"
;;
"delete")
sqlite3 $DB "DELETE FROM positions WHERE id=$2;"
;;
"export")
sqlite3 -csv -header $DB "SELECT * FROM positions;" > positions_$(date +%Y%m%d).csv
echo "导出完成"
;;
"stats")
sqlite3 $DB "SELECT
COUNT(*) as total,
COUNT(DISTINCT group_name) as groups,
datetime(MIN(create_time),'localtime') as first,
datetime(MAX(create_time),'localtime') as last
FROM positions;"
;;
*)
echo "用法: $0 {list|groups|search|delete|export|stats}"
echo " list - 列出所有位置"
echo " groups - 分组统计"
echo " search - 搜索关键词"
echo " delete - 删除记录"
echo " export - 导出CSV"
echo " stats - 统计信息"
;;
esac
使用:
bash
chmod +x db.sh
./db.sh list
./db.sh groups
./db.sh search 厦门
8. 图形化工具推荐对比
| 工具 | 安装方式 | 特点 |
|---|---|---|
| SQLiteBrowser | sudo apt install sqlitebrowser |
轻量、直观、免费 |
| DBeaver | sudo snap install dbeaver-ce |
功能强大、支持多数据库 |
| VS Code + SQLite扩展 | VS Code 扩展商店 | 开发方便、查询支持好 |
| DataGrip | 下载安装 | JetBrains产品、功能最全(付费) |
9. 最快速查看方式
bash
# 一行命令查看所有数据
sqlite3 -header -column ditu.sqlite "SELECT * FROM positions;"
# 查看统计信息
sqlite3 ditu.sqlite "SELECT
'总记录: ' || COUNT(*) || ' 条' as info
FROM positions
UNION ALL
SELECT
'分组: ' || GROUP_CONCAT(DISTINCT group_name)
FROM positions;"
推荐使用 SQLiteBrowser 图形化工具,既直观又易用。如果只是简单查看,命令行 sqlite3 就足够了。