赞
踩
由于sqlite非常精简,所以输出没有对齐,也没有表头,我们通常会设置如下:
.mode column // 输出对齐
.header on // 输出表头
.table // 查看表
有时需用 shell 脚本,调用 sqlite 来执行某sql文件,可通过如下语法
假设 src.txt 中 有一行是 k1: “v1”
# /bin/bash
set -e
k=$(grep "k1" src.txt | awk -F'"' '{print $2}') # 得到值为v1的变量
sqlite3 my.db <<EOF
UPDATE tb set name = '$k';
SELECT * FROM tb;
EOF
sqlite>.read a.sql
SELECT sql FROM sqlite_master WHERE type="table" AND name = "查询的表名";
若不存在则新建,否则打开已存在的。
bash> sqlite3 a.db # bash
sqlite> .open b.db # sqlite client
sqlite> .databases # 查看当前数据库文件路径
main: /home/testsqlite/a.db
导出完整的数据库到一个文本文件中:
root@deepglint:~/testsqlite# ls
a.db a.sql
root@deepglint:~/testsqlite# sqlite3 a.db .dump > 1.sql
root@deepglint:~/testsqlite# ll -h
total 2.1M
drwxr-xr-x 2 root root 4.0K May 4 10:33 ./
drwxr-xr-x 26 root root 4.0K May 4 10:22 ../
-rw-r--r-- 1 root root 460K May 4 10:33 1.sql
-rw-r--r-- 1 root root 444K May 4 10:22 a.db
-rw-r--r-- 1 root root 1.2M May 4 10:17 a.sql
将 a.sql 的内容,导入到 a.db
sqlite3 a.db < a.sql
sqlite3 database_name "VACUUM;"
sqlite 官方文档的并发性说明如下:,详见 https://www.sqlite.org/faq.html#q5
SQLite allows multiple processes to have the database file open at once, and for multiple processes to read the database at once. When any process wants to write, it must lock the entire database file for the duration of its update. But that normally only takes a few milliseconds. Other processes just wait on the writer to finish then continue about their business.
sqlite 内部对 database file 使用了锁
通过如下措施可以解决:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。