gugu15
Well-Known Member
- Joined
- Dec 15, 2014
- Messages
- 99
- Reaction score
- 63
Hello guys!
I'm building a interface with SQLITE3 and Lua on the otclient. But I want return a table with the results of the SELECT for use in Lua.
A table like:
I do not know how to do a function in c ++ to return a lua table
This is my code if somebody needs
I'm building a interface with SQLITE3 and Lua on the otclient. But I want return a table with the results of the SELECT for use in Lua.
A table like:
Code:
local selected = db:exec("SELECT * FROM PERSON")
--[
what I wanted:
selected = {{name = "Scot", age = 18, id = 1}, {name = "Louis", age = 21, id = 2}}
]]
I do not know how to do a function in c ++ to return a lua table
This is my code if somebody needs
C++://header #ifndef __SQLAPI_H__ #define __SQLAPI_H__ #include <sqlite3.h> #include "sqlitedeclaration.h" #include <framework/luaengine/luaobject.h> #include <framework/luaengine/luainterface.h> class DatabaseSqlite : public LuaObject { public: static int callback(void *data, int argc, char **argv, char **columnName); bool exec(const std::string& query); bool open(const std::string& name); void onExec(std::string column, std::string value, int count, int total); void close(); protected: sqlite3 *database; }; #endif
Running:C++:#include "sqlapi.h" int DatabaseSqlite::callback(void *p, int argc, char **argv, char **columnName){ DatabaseSqlite* pthis = (DatabaseSqlite*)(p); for(int i = 0; i<argc; i++){ pthis->onExec(std::string(columnName[i]), std::string(argv[i] ? argv[i] : "NULL"), i+1, argc); } return 0; } void DatabaseSqlite::onExec(std::string column, std::string value, int count, int total){ callLuaField("onExec", column, value, count, total); } bool DatabaseSqlite::exec(const std::string& query){ char *errorMsg = 0; int ret; ret = sqlite3_exec(database, query.c_str(), DatabaseSqlite::callback, (void*)this, &errorMsg); if (ret == SQLITE_OK){ return true; }else{ sqlite3_free(errorMsg); return false; } } bool DatabaseSqlite::open(const std::string& name){ // .db name file is the paramter int ret; ret = sqlite3_open(name.c_str(), &database); if (ret == SQLITE_OK){ return true; }else{ return false; } } void DatabaseSqlite::close(){ sqlite3_close(database); }