[Kepler-Project] LuaSQL Usage

Bertrand Mansion golgote at mamasam.com
Mon May 14 05:34:22 BRT 2007


Le 14 mai 07 à 09:19, Bertrand Mansion a écrit :

> Here is a version for MySQL which I haven't tested yet:
>
> static int conn_quote(lua_State *L)
> {
>     const char *unquoted = luaL_checkstring(L, 2);
>     size_t unquotedlen = strlen(unquoted);
>     size_t quotedlen;
>     conn_data *conn = getconnection (L);
>     char *quoted = (char *)calloc(sizeof(char),(unquotedlen * 2) + 1);
>     quotedlen = mysql_real_escape_string(conn->my_conn, quoted,  
> unquoted, unquotedlen);
>     lua_pushstring(L, quoted);
>     free(quoted);
>     return 1;
> }

After some tests, this one will be better since like the sqlite3 one,  
it adds ' around the string:

static int conn_quote(lua_State *L)
{
     const char *unquoted = luaL_checkstring(L, 2);
     size_t unquotedlen = strlen(unquoted);
     size_t quotedlen;
     conn_data *conn = getconnection (L);
     char *quoted = malloc(2 * unquotedlen + 3);
     quotedlen = mysql_real_escape_string(conn->my_conn, quoted + 1,  
unquoted, unquotedlen);
     quoted[0] = quoted[++quotedlen] = '\'';
     quoted[++quotedlen] = '\0';
     lua_pushstring(L, quoted);
     free(quoted);
     return 1;
}




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





More information about the Kepler-Project mailing list