I'm trying to write a custom query function that will handle errors and shit like that. At the moment, it does not return any value (even though, in theory it should), so I am a bit stuck.
The function:
[LUA]
-- Type 1 = single value, type 2 = multi-value
function DB:Query( sql_query, sql_type )
if ( self.db:status() != mysqloo.DATABASE_CONNECTED ) then return error( "No SQL database connected, failed to run query." ) end
if ( type( sql_query ) != "string" ) then return error( "SQL query is not a string." ) end
local query = self.db:query( sql_query )
query.onError = function( q, err )
self:Notify( tostring( q ) .. " returned an error: " .. err )
end
query:start()
if ( query:isRunning() ) then
local data = query:getData()
if ( data[1] != nil ) then
if ( sql_type == 1 ) then
return data[1]
elseif ( sql_type == 2 ) then
return data
end
end
end
end
[/LUA]
Also when the player first spawns, I run this meta function, but as the queries just return nil everything breaks.
[LUA]
function PLAYER:InitVars()
if ( !self or !self:IsValid() ) then return end
if ( SERVER ) then
self.rp_name_first = DB:Query( "SELECT rp_name_first FROM frp_users WHERE steamid = '" .. self:SteamID() .. "';", 1 )
self.rp_name_last = DB:Query( "SELECT rp_name_last FROM frp_users WHERE steamid = '" .. self:SteamID() .. "';", 1 )
self.money_wallet = DB:Query( "SELECT money_wallet FROM frp_users WHERE steamid = '" .. self:SteamID() .. "';", 1 )
self.xp = DB:Query( "SELECT xp FROM frp_users WHERE steamid = '" .. self:SteamID() .. "';", 1 )
self.time_played = DB:Query( "SELECT time_played FROM frp_users WHERE steamid = '" .. self:SteamID() .. "';", 1 )
self.money_wallet = tonumber( self.money_wallet, 10 )
self.xp = tonumber( self.xp, 10 )
self.time_played = tonumber( self.time_played, 10 )
MsgN( type(self.rp_name_first) .. " " .. self.rp_name_first)
MsgN( type(self.rp_name_last) .. " " .. self.rp_name_last )
MsgN( type(self.money_wallet) .. " " .. self.money_wallet )
MsgN( type(self.xp) .. " " .. self.xp )
MsgN( type(self.time_played) .. " " .. self.time_played )
end
end
[/LUA]
Any help would be appreciated.
The data is only available when query:onSuccess is called. You can't return data from mysqloo like that.
[QUOTE=sannys;51167971]The data is only available when query:onSuccess is called. You can't return data from mysqloo like that.[/QUOTE]
I tried doing that before, and it never worked then either.
[QUOTE=Dj6619;51167983]I tried doing that before, and it never worked then either.[/QUOTE]
Well, you did it wrong. Should look like this.
[lua]local query = self.db:query(sql_query)
query.onError = function(q, err)
self:Notify(tostring(q) .. " returned an error: " .. err )
end
query.onSuccess = function(q, data)
PrintTable(data)
end
query:start()[/lua]
[QUOTE=sannys;51168013]Well, you did it wrong. Should look like this.
[lua]local query = self.db:query(sql_query)
query.onError = function(q, err)
self:Notify(tostring(q) .. " returned an error: " .. err )
end
query.onSuccess = function(q, data)
PrintTable(data)
end
query:start()[/lua][/QUOTE]
Hmm, then I assume you'd just return the variable data or use some form of object notation?
[QUOTE=Dj6619;51168028]Hmm, then I assume you'd just return the variable data or use some form of object notation?[/QUOTE]
You can't return the data at all. You can store it somewhere, but you can't return it. The easiest thing to do would be to select * and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/table/Merge]table.Merge[/url] the data into the player. I recommend creating a table inside the player to store all of your info, too, so it doesn't conflict with anything else. player.xp is likely to conflict with other addons.
[QUOTE=sannys;51168076]You can't return the data at all. You can store it somewhere, but you can't return it. The easiest thing to do would be to select * and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/table/Merge]table.Merge[/url] the data into the player. I recommend creating a table inside the player to store all of your info, too, so it doesn't conflict with anything else. player.xp is likely to conflict with other addons.[/QUOTE]
Would you mind giving me a code example? I've been stuck on this issue for a while :)
[lua]function DB:Query(sql_query, callback)
local query = self.db:query(sql_query)
query.onError = function( q, err )
self:Notify( tostring( q ) .. " returned an error: " .. err )
end
query.onSuccess = function(q, data)
callback(data)
end
query:start()
end[/lua]
[lua]
local PLAYER = FindMetaTable("Player")
function PLAYER:InitVars()
self.myAddonData = self.myAddonData or {}
DB:Query("SELECT * FROM frp_users WHERE steamid = '"..self:SteamID().."';", function(data)
if (#data > 0) then
data = data[1] -- data is a table of results. Get the first result.
if (IsValid(self)) then -- The player might be invalid.
table.Merge(self.myAddonData, data)
end
else
-- This player has no data.
end
end)
end[/lua]
Then you can access your data through the ply.myAddonData table
So for example, the xp would be ply.myAddonData.xp
[QUOTE=sannys;51168208][lua]function DB:Query(sql_query, callback)
local query = self.db:query(sql_query)
query.onError = function( q, err )
self:Notify( tostring( q ) .. " returned an error: " .. err )
end
query.onSuccess = function(q, data)
callback(data)
end
query:start()
end[/lua]
[lua]
local PLAYER = FindMetaTable("Player")
function PLAYER:InitVars()
self.myAddonData = self.myAddonData or {}
DB:Query("SELECT * FROM frp_users WHERE steamid = '"..self:SteamID().."';", function(data)
if (#data > 0) then
data = data[1] -- data is a table of results. Get the first result.
if (IsValid(self)) then -- The player might be invalid.
table.Merge(self.myAddonData, data)
end
else
-- This player has no data.
end
end)
end[/lua]
Then you can access your data through the ply.myAddonDaa
So for example, the xp would be ply.myAddonData.xp[/QUOTE]
Okay this makes more sense, thanks for the help. I will try to implement this into my current system and see how it goes.
Sorry, you need to Log In to post a reply to this thread.