小问题,但令人恼火:有没有办法避免每次查询时出现以下消息:
-- 从 /Users/ThG/.sqliterc 加载资源
作为一个愚蠢的解决方法,这是有效的:
<. sqlite your_sqlite.db 'select * from your_table'
这是因为当前代码执行以下操作:
if( stdin_is_interactive ){
utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
}
强制标准输入重定向会阻止此操作,因为这段代码:
stdin_is_interactive = isatty(0);
这也有效:
sqlite -batch your_sqlite.db 'select * from your_table'
由于这段代码:
}else if( strcmp(z,"-batch")==0 ){
/* Need to check for batch mode here to so we can avoid printing
** informational messages (like from process_sqliterc) before
** we do the actual processing of arguments later in a second pass.
*/
stdin_is_interactive = 0;
}
但它更长,所以有点违背了目的。
我知道这个问题现在已经很老了,但只需删除“/Users/ThG/.sqliterc”就可以解决问题。 '.sqliterc' 是 sqlite 交互式命令行前端的配置文件。如果您没有花很多时间在那里,您就不会错过该文件。
有点晚了,但@levant pied几乎有了解决方案,你需要传递一个额外的-interactive来静默--loading资源。
$ sqlite3 -batch -interactive
SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> .q
**编辑 2024 年 1 月
使加载消息静音的另一种方法是:
$ sqlite3 2>/dev/null
SQLite version 3.44.2 2023-11-24 11:41:44
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite>
该资源消息出现在 stderr 上,后面跟着一个空行,因此您可以使用类似这样的内容删除它(包含在自己的脚本文件中):
#!/bin/bash
sqlite3 -init /your/init/file /your/sqlite3/file.db "
your
SQL
cmds
" 2>/dev/null | sed -e1d
在 shell 脚本中使用 sqlite 时,您通常根本不希望加载
~/.sqliterc
。这对我来说效果很好:
sqlite3 -init <(echo)
说明:
-init
指定要加载的文件,而不是 ~/.sqliterc
。<(echo)
使用 Process Substitution 提供临时空文件的路径。您只需重命名配置文件即可禁用警告。并恢复重命名以在使用后保留配置。
我使用以下内容:
#suppress default configuration warnings
mv $HOME/.sqliterc $HOME/.backup.sqliterc
# sqlite3 scripts...
#revert
mv $HOME/.backup.sqliterc $HOME/.sqliterc