• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Function mysqlQuery in otserv!

AzLaN

New Member
Joined
Jun 6, 2008
Messages
89
Reaction score
3
Yeah since I needed a lua function that queries stuff to the sql server I made this function. It doesn't have any kind of error checking. I'd be really happy if someone fixed that for me.

First add database.h to your includes!

in: luascript.cpp add:
Code:
int LuaScriptInterface::luaMysqlQuery(lua_State *L)
{
    //mysqlQuery(query, data/info)
    DBQuery query;
    std::string data = popString(L);
    query << popString(L);
    Database* db = Database::instance();
    DBResult* result;

    if(!(result = db->storeQuery(query.str()))){
        return false;
    }
    if(data != "set")
    {
        std::vector<std::string> dataexp = explodeString(data, ", ");
        lua_createtable(L, dataexp.size(), 0);

        for (int i=0; i<(int)dataexp.size(); ++i)
        {
            setField(L, dataexp[i], result->getDataString(dataexp[i]));
        }
    }
    query.str("");
    return 1;
}
after
Code:
int LuaScriptInterface::luaGetPlayerIp(lua_State *L)
{
    return internalGetPlayerInfo(L, PlayerInfoIp);
}

and then add:

Code:
void LuaScriptInterface::setField(lua_State *L, std::string index, const std::string& val)
{
    lua_pushstring(L, index.c_str());
    lua_pushstring(L, val.c_str());
    lua_settable(L, -3);
}

after

Code:
void LuaScriptInterface::setField(lua_State *L, const char* index, int32_t val)
{
    lua_pushstring(L, index);
    lua_pushnumber(L, (double)val);
    lua_settable(L, -3);
}

in: luascript.h add:

Code:
static int luaMysqlQuery(lua_State *L);

before

Code:
static int luaGetConfigValue(lua_State *L);

and then add:

Code:
static void setField(lua_State *L, std::string index, const std::string& val);

after:

Code:
static void setField(lua_State *L, const char* index, int32_t val);

DONE :).


I hope I didn't forget anything and here is a example usage:
Code:
function onSay(cid, words, param)
    s = mysqlQuery("SELECT `online`, `force` FROM `players` WHERE `players`.`name` = '" .. getPlayerName(cid) .. "'", "force, online")
    broadcastMessage(s.force)
    mysqlQuery("INSERT INTO `accounts` (`name`, `password`) VALUES (5, 5)", "set")
end

I hope you can see how it works.
 
Amazing! Thank you a bunch!
Also, if you want to add it as a TalkAction command for UPDATE/INSERT/DELETE, you can do this:

data\talkactions\talkactions.xml
Lua:
<talkaction words="/sql" access="3" filter="first word" separator=" " script="sql.lua"/>

data\talkactions\scripts\sql.lua
Lua:
function onSay(cid, words, param)
    local query = string.gsub(param, "'", "''")
    if (query ~= "") then
        mysqlQuery(query, "set")
    end
   
    print(":: Executed SQL Query by [" .. getCreatureName(cid) .. "]: " .. query);
    return 1
end

Usage in the game:
Code:
/sql update players set level = 123 where name = 'Steve'
/sql delete from bans where id = 5
/sql insert into TABLE_NAME (x,y,z) VALUES (x,y,z)

So now you can run SQL queries from the game as a gamemaster.
The script will also take into consideration with character names that includes the ' character so it won't mess up the queries.
 
Back
Top