Jump to content

Using SQLite


Gravestone

Recommended Posts

Hello everyone. I am creating something to save team kills when the resource stops. The kills are successfully saved but I want to load them when resource starts back, I mean the db is connected and sets the element (team) data to which it had before the resource had stopped.

The coloumn in which the kills are stored is known as "Kills" and where the team name is before that column named "teamname".

I tried this:

addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), 
    function () 
        for i, team in ipairs(getElementsByType("team")) do 
        local name = getTeamName(team) 
        local data = getTeamData(name) 
        outputChatBox(tostring(false)) 
        setElementData(team, "Kills", data) 
    end 
end 
) 
function getTeamData(team) 
    local results = dbPoll( dbQuery( connection, "SELECT * FROM claninfo WHERE teamname = ?", teamname), -1 ) 
    if type(results) == "table" and #results == 0 or not results then 
        return false 
    else 
        return results[2]["Kills"] 
    end 
end 

Error: it is returning false

Edited by Guest
Link to comment

Alright so now I want to get all the data in the database even if the team doesn't exist. Table name: teaminfo Colum1: kills

I load the team data when the resource starts but how do I load the data for the teams who don't exist but they were saved when the resource stopped?

Link to comment
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), 
function () 
    for i, team in ipairs(getElementsByType("team")) do 
        local name = getTeamName(team) 
        local data = getTeamData(name) 
        if data ~= false then 
            outputChatBox(tostring(false)) 
            setElementData(team, "Kills", data) 
        end 
    end 
end 
) 
function getTeamData(team) 
    local results = dbPoll( dbQuery( connection, "SELECT * FROM claninfo WHERE teamname = ?", team), -1 ) 
    if type(results) == "table" and #results == 0 or not results then 
        return false 
    else 
        return results[2]["Kills"] 
    end 
end 

Link to comment
If they're would been saved, they would exist. Explain better what are you tring to do, please.

I'm trying to say that two teams exist in the server, Ballas and Grove. I destroy one team by /destroyteam Grove so now only Ballas exists. I want to load the data for Grove as well and output it to the chatbox, how do I?

Link to comment
function getTeams()
    local results = dbPoll( dbQuery( connection, "SELECT * FROM teaminfo WHERE teamname = ?"), -1 )
	
    if type(results) == "table" and #results == 0 or not results then
        return false
    else
	
        return results[1]["teamname"]
		
    end
end
function doo()
	local names =  getTeams()
	for i, v in ipairs(names) do
		outputChatBox(v)
		end
	end
addCommandHandler("teams", doo)

Tried this but the returns of the getTeams function are false even tho the table name is teaminfo and column name is teamname.

Link to comment
local results = dbPoll( dbQuery( connection, "SELECT * FROM teaminfo WHERE teamname = ?", Added team name here ), -1 ) -- added the team name

Or try to use something like this .

function getTeams()
  local data = dbPoll(dbQuery(connection,"SELECT * FROM teaminfo"), -1)
  return data
end

function doo(player)
	local names =  getTeams()
	if names then 
		for i, v in ipairs(names) do
			outputChatBox(v.teamname)
		end
	end
end 
addCommandHandler("teams", doo)

 

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...