Lua MySQL,需要一种转义数据的方法 [英] Lua mysql, need a way to escape data

查看:294
本文介绍了Lua MySQL,需要一种转义数据的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种在lua中为mysql语句转义数据的方法.我曾经在php中做类似mysql_real_escape_string()的事情,但是在使用MySQL的lua中找不到等效项(当我使用sqlite3时,con:escape()起作用了).我已经读过,准备好的语句是一种解决方案,但它似乎对我不起作用.我在做什么错了?

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"

此错误为试图调用方法'prepare'(nil值)".

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

如果我尝试在con上运行直接的SELECT *,它可以正常工作,因此可以建立连接,但是此prepare语句不起作用(看来甚至都没有将prepare识别为有效方法).

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).

推荐答案

好像在过去一两年内,在LuaSQL中添加了prepare功能,所以您的版本可能更老了吗?

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

此外,尝试con:escape(yourQuery)进行转义,也许这足以满足您的需求.

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

这篇关于Lua MySQL,需要一种转义数据的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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