LuaSQL,它似乎是 Lua 中大多数 SQL 数据库系统的规范库,但似乎并不存在具有在查询中引用/转义值的任何设施。 我正在编写一个使用 SQLite 作为后端的应用程序,并且我希望使用 Python 的 DB-API:
c.execute('select * from stocks where symbol=?', t)
但我什至会选择更愚蠢的东西,比如:
conn:execute("select * from stocks where symbol=" + luasql.sqlite.quote(t))
还有其他支持 SQLite 引用的 Lua 库吗? (LuaSQLite3 似乎没有。)或者我是否遗漏了有关 LuaSQL 的某些内容? 我担心滚动我自己的解决方案(使用正则表达式或其他东西)并出错。 我应该为 sqlite3_snprintf 编写一个包装吗?
LuaSQL, which seems to be the canonical library for most SQL database systems in Lua, doesn't seem to have any facilities for quoting/escaping values in queries. I'm writing an application that uses SQLite as a backend, and I'd love to use an interface like the one specified by Python's DB-API:
c.execute('select * from stocks where symbol=?', t)
but I'd even settle for something even dumber, like:
conn:execute("select * from stocks where symbol=" + luasql.sqlite.quote(t))
Are there any other Lua libraries that support quoting for SQLite? (LuaSQLite3 doesn't seem to.) Or am I missing something about LuaSQL? I'm worried about rolling my own solution (with regexes or something) and getting it wrong. Should I just write a wrapper for sqlite3_snprintf?
发布评论
评论(3)
我有一段时间没看过 LuaSQL 但上次我检查它不支持它。 我使用Lua-Sqlite3。
I haven't looked at LuaSQL in a while but last time I checked it didn't support it. I use Lua-Sqlite3.
LuaSQLite3 以及任何其他到 SQLite 的低级绑定都提供带有可变参数的准备好的语句; 它们使用方法将值绑定到语句参数。 由于 SQLite 不解释绑定值,因此根本不可能发生 SQL 注入。 这是迄今为止最安全(且性能最佳)的方法。
uroc 显示了将绑定方法与准备好的语句一起使用的示例。
LuaSQLite3 as well an any other low level binding to SQLite offers prepared statements with variable parameters; these use methods to bind values to the statement parameters. Since SQLite does not interpret the binding values, there is simply no possibility of an SQL injection. This is by far the safest (and best performing) approach.
uroc shows an example of using the bind methods with prepared statements.
顺便说一句,在 Lua SQL 中,conn:escape 中有一个用于 sqlite3 驱动程序的未记录的转义函数,其中 conn 是一个连接变量。
例如,代码的
结果是:
我实际上尝试了看看它会做什么。 显然他们的 postgres 驱动程序也有这样的功能。 我通过查看他们的测试发现了这一点。
希望这可以帮助。
By the way in Lua SQL there is an undocumented escape function for the sqlite3 driver in conn:escape where conn is a connection variable.
For example with the code
the result is:
I actually tried that to see what it'd do. Apparently there is also such a function for their postgres driver too. I found this by looking at the tests they had.
Hope this helps.