我有几个基本脚本,它们在填充我的 sqlite3 数据库时踢出一些信息,但大约有一半的时间命令立即失败:
$ sqlite3 outgoing.db "select * from edges where worker is not null;"
Error: database is locked
$ sqlite3 outgoing.db "select * from edges where worker is not null;"
Error: database is locked
$ sqlite3 outgoing.db "select * from edges where worker is not null;"
1014->9000|1014|9000||-1.0|2
1014->9001|1014|9001||-1.0|2
...
Run Code Online (Sandbox Code Playgroud)
如果我添加.timeout 1;到命令的开头,我只会收到一个语法错误;如何.通过命令行以非交互方式向sqlite 传递特殊参数?
只需使用:
$ sqlite3 -cmd ".timeout 1000" outgoing.db "SELECT * FROM edges"
Run Code Online (Sandbox Code Playgroud)
您可以通过使用 init 文件来做到这一点。
init.sql (请注意,超时值以毫秒为单位 - 1 相当短):
.timeout 1000
Run Code Online (Sandbox Code Playgroud)
在提示下:
$ sqlite3 -init init.sql outgoing.db "select * from edges where worker is not null;"
Loading resources from init.sql
# 1 second pause
Error: database is locked
Run Code Online (Sandbox Code Playgroud)
使用某些 shell(至少在 Linux 上,不是很便携),如果这是一个问题,您可以避免需要带有进程替换的正确文件:
$ sqlite3 -init <(echo .timeout 1000) your.db "your sql;"
Run Code Online (Sandbox Code Playgroud)
如果输出被重定向到文件或管道,或者.output如果您在 init 文件中指定了文件或管道,则不会打印额外的输出行(“从...加载资源”)。