如何检查 SQLite3 语法?
有没有办法在不运行 SQLite3 脚本的情况下检查它的语法?
基本上,我正在寻找与 ruby -c script.rb、perl -c script.pl、php --syntax-check 脚本等效的 SQLite3。 )
我曾考虑过使用 explain
,但我想检查的大多数脚本都保留用于参考目的(并且不一定有关联的数据库 )。使用 explain
也会使其难以与 等内容一起使用综合。 (也就是说,我只想检查语法,而不是语义。)
更新:
我很困惑。假设我想对此进行语法检查:
select * from foo;
我可以这样做:
echo 'explain select * from foo;' | sqlite3
但是然后我得到:
SQL error near line 1: no such table: foo
我期望看到的是“语法正常”(或类似的内容)。这可能吗?
Is there a way to check the syntax of a SQLite3 script without running it?
Basically, I'm looking for the SQLite3 equivalent of ruby -c script.rb
, perl -c script.pl
, php --syntax-check script.php
, etc.
I've thought of using explain
, but most of the scripts I'd like to check are kept around for reference purposes (and don't necessarily have an associated database). Using explain
would also make it hard to use with something like Syntastic. (That is, I'm only wanting to check syntax, not semantics.)
Update:
I'm confused. Let's say I want to syntax check this:
select * from foo;
I could do something like:
echo 'explain select * from foo;' | sqlite3
But then I get:
SQL error near line 1: no such table: foo
All I'd expect to see is "Syntax OK" (or something similar). Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
到目前为止,对我来说效果很好的语法检查器是 Ruby gem
sqlint
它检查 ANSI SQL:它不是特定于
sqlite
方言。它于 2015 年发布,也就是问题提出 5 年后
它需要 Ruby 和 C 编译器(用于构建
pg_query
本机扩展。)以下是输出示例:
<代码>
$sqlint ex7a.sql
ex7a.sql:81:10:ERROR “*”处或附近的语法错误
在真正的 Unix 传统中,如果语法正确,sqlint 不会产生任何输出。正如提问者所问,它不检查表是否存在。
A syntax checker that works well for me so far is the Ruby gem
sqlint
It checks ANSI SQL: it is not specific to the
sqlite
dialect.It came out in 2015, 5 years after the question was asked
It needs Ruby and a C compiler (to build the
pg_query
native extension.)Here is an example of output:
$ sqlint ex7a.sql
ex7a.sql:81:10:ERROR syntax error at or near "*"
In the true Unix tradition, if the syntax is correct, sqlint produces no output. As the questioner asked, it doesn't check if tables exist.
正如您所提到的,您可以使用 EXPLAIN 关键字。如果您省略了前面的 EXPLAIN 关键字,它将返回有关如何执行该语句的信息。
As you mentioned, you can use the EXPLAIN keyword. It will return information about how the statement would be executed had you omitted the preceding EXPLAIN keyword.
您可能可以对内存数据库使用 EXPLAIN。使用 sqlite3,您可以通过将“:memory:”作为文件名传递给 sqlite3_open_v2() 来获取内存数据库。
You could probably use EXPLAIN against an in memory database. With sqlite3, you can get an in memory database by passing ":memory:" as the filename to sqlite3_open_v2().