如何引用LuaSQL的值? [英] How to quote values for LuaSQL?

查看:135
本文介绍了如何引用LuaSQL的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LuaSQL ,这似乎是Lua中大多数SQL数据库系统的规范库,不在查询中似乎没有任何引用/转义值的功能。我正在编写一个使用SQLite作为后端的应用程序,我很乐意使用一个如 Python的DB-API

  c.execute('select * from stocks其中symbol =?',t)

  conn:execute(select * from stocks where symbols =+ luasql.sqlite.quote(t))$ b $有没有其他的Lua库支持引用SQLite? ( LuaSQLite3 似乎没有。)还是我缺少关于LuaSQL的内容?我担心滚动自己的解决方案(用正则表达式或某些东西),并且错误。我应该写一个 sqlite3_snprintf 的包装器吗?

解决方案

我没有看过LuaSQL一段时间,但上次我检查它不支持它。我使用Lua-Sqlite3。

  require(sqlite3)

db = sqlite3.open_memory )

db:exec [[CREATE TABLE tbl(first_name TEXT,last_name TEXT); ]]

stmt = db:prepare [[INSERT INTO tbl(first_name,last_name))VALUES(:first_name,:last_name)]]

stmt:bind({first_name = hawkeye,last_name =pierce}):exec()
stmt:bind({first_name =henry,last_name =blake}):exec()

r in db:rows(SELECT * FROM tbl)do
print(r.first_name,r.last_name)
end


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?

解决方案

I haven't looked at LuaSQL in a while but last time I checked it didn't support it. I use Lua-Sqlite3.

require("sqlite3")

db = sqlite3.open_memory()

db:exec[[ CREATE TABLE tbl( first_name TEXT, last_name TEXT ); ]]

stmt = db:prepare[[ INSERT INTO tbl(first_name, last_name) VALUES(:first_name, :last_name) ]]

stmt:bind({first_name="hawkeye", last_name="pierce"}):exec()
stmt:bind({first_name="henry", last_name="blake"}):exec()

for r in db:rows("SELECT * FROM tbl") do
    print(r.first_name,r.last_name)
end

这篇关于如何引用LuaSQL的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆