[Kepler-Project] LuaSQL Usage

Bertrand Mansion golgote at mamasam.com
Mon May 14 04:00:27 BRT 2007

Le 6 mai 07 à 21:17, Richard Warburton a écrit :

> Bertrand Mansion wrote:
>>>               function() return "'"..sGsub(tRemove(vars,1),"'","\ 
>>> \'").."'" end
>> Isn't there a 'quote' or 'escape' function in LuaSQL that could  
>> replace this last function ?
>> IIRC, some database quote with '' while others quote with \'.
> If there is, I can't find it: http://www.keplerproject.org/luasql/ 
> manual.html
> Hence my email.  :-)

Here is a quote version for sqlite3:

static int conn_quote(lua_State *L)
     const char *unquoted = luaL_checkstring(L, 2);
     size_t unquotedlen = strlen(unquoted);
     char *quoted = malloc(2 * unquotedlen + 3);
     sqlite3_snprintf(2 * unquotedlen + 3, quoted, "'%q'", unquoted);
     lua_pushstring(L, quoted);
     return 1;

I'm not sure it is perfect since I am not a real C coder nor a Lua C  
API master, but tests pass.
I will try to write a version for MySQL. I don't have access to other  
databases at the moment.

Quote is important in Luasql because databases use different escape  
sequences and because there are more characters than "'" that need to  
be escaped (ex: \x00, \n, \r, \, ', " and \x1a for MySQL).  
Furthermore, escaping sometimes depends on the connection charset.

Bertrand Mansion
Work : http://www.mamasam.com
Blog : http://golgote.freeflux.net

More information about the Kepler-Project mailing list