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:
after
and then add:
after
in: luascript.h add:
before
and then add:
after:
DONE .
I hope I didn't forget anything and here is a example usage:
I hope you can see how it works.
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;
}
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.