Jump to content

[Help] SQL Select Problem


steadyfi

Recommended Posts

Hello

Can someone tell me how can I convert a table into a integer value ?

I'm using SQL Select function and it returns me a table even if I check in a table where is only a SINGLE registry.

When I say a single registry, i mean the Account Name doesn't get repeated, and that's what I'm checking for and then I get the rank. How can I transform this table into a number ?

Long story short, how do I convert a table with a single element into a integer value ?

I know I need to use:

tonumber 
executeSQLQuery 
for 
if 

My Code:

executeSQLQuery("SELECT rank FROM otplayers WHERE player=? AND team=?", getAccountName(getPlayerAccount(player)), getTeamName(getPlayerTeam(player))) 

Thanks :D

Link to comment

If there is only one row returned then the table returned should look something like this:

returned_tabke = { 
 [1] = { 
  ["rank"] = "RANK" 
 } 
} 

Why are you still using executeSQLQuery? Use dbQuery instead.

Link to comment
If there is only one row returned then the table returned should look something like this:
returned_tabke = { 
 [1] = { 
  ["rank"] = "RANK" 
 } 
} 

Why are you still using executeSQLQuery? Use dbQuery instead.

Thank you JR10 !

I'm using executeSQLQuery because I realised I don't need a private database and because my script is already completely Non-Functional from the change I made from Account Data to SQL + the table oriented Gridlist that shows all the players in the team including offline ones, now I need to fix all the functions and repair all the SQL + make the code more compact and better. Also speed is a problem.

Conclusion: Everything is broken so there is no need to change db + registry.db is good enough

Wish me luck ;)

Link to comment
db* functions are not slower in any way, so "speed" shouldn't be a problem. It's generally better that you create a private local database to the resources folder.

Good luck.

I wasn't saying the DB is slow, the events are lame and there is a slight delay in the GUIs + sometimes they need to be reopened. Anyway, thanks man. :D

Link to comment

Depends on the variable that is assigned to the retval of dbPoll.

local result = dbPoll(queryHandle, 0) 
local rank = result[1].rank -- Rank of the first row 
local rank = result[2].rank -- Rank of the second row 
rank = tonumber(rank) 
triggerClientEvent(target, "event", target, rank) 

Link to comment
Depends on the variable that is assigned to the retval of dbPoll.
local result = dbPoll(queryHandle, 0) 
local rank = result[1].rank -- Rank of the first row 
local rank = result[2].rank -- Rank of the second row 
rank = tonumber(rank) 
triggerClientEvent(target, "event", target, rank) 

Like this ?

local result = executeSQLQuery("SELECT rank FROM otplayers WHERE player=? AND team=?", getAccountName(getPlayerAccount(player)), getTeamName(getPlayerTeam(player))) 
local rank = result[1].rank 
rank = tonumber(rank) 
triggerClientEvent(target, "event", target, rank) 

Im still using executeSQLQuery, too bored right now to change it :roll:

Link to comment
Yes this should work.

If you're only expecting one row, you can append 'LIMIT 1' to the query string, this can significantly improve performance as it will halt when it has found one row.

executeSQLQuery("SELECT rank FROM otplayers LIMIT 1 WHERE player=? AND team=?", getAccountName(getPlayerAccount(player)), getTeamName(getPlayerTeam(player))) 

Is it correct ?

Link to comment
Yes this should work.

If you're only expecting one row, you can append 'LIMIT 1' to the query string, this can significantly improve performance as it will halt when it has found one row.

executeSQLQuery("SELECT rank FROM otplayers LIMIT 1 WHERE player=? AND team=?", getAccountName(getPlayerAccount(player)), getTeamName(getPlayerTeam(player))) 

Is it correct ?

executeSQLQuery("SELECT rank FROM otplayers WHERE player=? AND team=? LIMIT 1;", getAccountName(getPlayerAccount(player)), getTeamName(getPlayerTeam(player))) 

Link to comment

Ok, i ran into another problem.

I tried some debugging, no problems in handlers or source element. I opened the SQL file, there were all the tables. The problem is in the SQL Function.

addEvent("openteams.gui.playerOpensGUI", true) 
addEventHandler("openteams.gui.playerOpensGUI", getRootElement(), function() 
    local team = getPlayerTeam(source) 
    if team then 
        --Player Rank 
            local playerRank = executeSQLQuery("SELECT rank FROM otplayers WHERE player=? AND team=?", getAccountName(getPlayerAccount(source)), getTeamName(getPlayerTeam(source))) 
            local rank = playerRank[1].rank 
            local rank = tonumber(returnRank) 
            outputChatBox("Rank "..tostring(rank))  --IGNORE, DEBUGGING 
        --Leader Name 
            local leaderName = executeSQLQuery("SELECT leader FROM otteams WHERE team=?", getTeamName(team)) 
            local name = leaderName[1].name 
            local name = tostring(returnName) 
            outputChatBox("Leader "..tostring(name))  --IGNORE, DEBUGGING 
            --Event 
                triggerClientEvent(source, "openteams.gui.playerOpensGUI.return", source, returnRank, returnName) 
    end 
end) 

The event get's triggered by a function Client Side,

triggerServerEvent("openteams.gui.playerOpensGUI", localPlayer) 

And then it bounces back client side to open the GUI with the required information.

Anyone ?

Link to comment
At line 11 you selected leader and at line 12 you tried to get name but you haven't selected it.
local rank = tonumber(returnRank) 

returnRank is not defined, should be just 'rank', the same goes for returnName.

Thank you guys, your posts helped me !

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...