Linux Ubuntu 查看 SQLite 工具

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 扩展
  1. 安装扩展 "SQLite" by alexcvzz
  2. 打开数据库文件,右键选择 "Open Database"
  3. 可以查看和查询数据

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 就足够了。

相关推荐
Lsetea2 小时前
OpenSSL的checkhost显示不匹配却退出0:名称检查与证书验收
linux·https·shell·ssl证书·openssl
张文君2 小时前
Ubuntu iSCSI CHAP认证与Win11挂载
linux·运维·ubuntu
一直C2 小时前
嵌入式 Linux 驱动开发学习笔记:中断、等待队列与工作队列
linux·驱动开发·嵌入式硬件·学习·imx6ull
程序员-Benothing2 小时前
Linux防火墙配置教程:iptables、firewalld、nftables、ufw
linux·运维·服务器
齐鲁大虾2 小时前
如何通过 CUPS Web 界面(http://localhost:631)安装打印机驱动
linux
yunwei372 小时前
eBPF 实例教程:使用内核跟踪点监控 GPU 驱动活动
linux·后端·性能优化
AlfredZhao3 小时前
用脚本按顺序执行初始化 DB 的 SQL 文件
linux
瑞克-桑切斯4 小时前
企业生产环境:https部署+nginx高可用(业务不中断)
linux·服务器·数据库
浩浩测试一下11 小时前
Linux 底层知识点 1
linux·运维·服务器