Lua mysql, need a way to escape data

孤街醉人 提交于 2019-12-23 18:23:22

问题


I need a way to escape data for mysql statements in lua. I'm used to doing something like mysql_real_escape_string() in php but can't find an equivalent in lua using mysql (con:escape() worked when I was using sqlite3). I've read that prepared statements are a solution but it doesn't seem to work for me. What am I doing wrong?

require "luasql.mysql"
env = assert (luasql.mysql())
con = env:connect("db_name", "user", "pass", "localhost")
local stmt = con:prepare([[
    SELECT * FROM `user` 
    WHERE `login` = :a AND `pass` = :b LIMIT 1
]])
stmt.a = "some_user"
stmt.b = "some_pass"

This errors with "attempt to call method 'prepare' (a nil value)".

If I try to run a straight SELECT * execute on con it works fine, so the connection is being made, but this prepare statement does not work (it's not even recognizing prepare as a valid method, it seems).


回答1:


It looks like the prepare functionality was added to LuaSQL within the last year or two, so maybe you version is a bit older?

Also, try con:escape(yourQuery) to do the escaping, maybe that will be sufficient for your needs.



来源:https://stackoverflow.com/questions/6318525/lua-mysql-need-a-way-to-escape-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!