Jump to content

[Help] Mysql Column insert


Syntrax#

Recommended Posts

You could either store it as JSON or create a primary key - foreign key relation between tables. Eg. 

Teams:

ID: 1, Name: MyTeam

ID: 2, Name: YourTeam

TeamRanks:

TeamID: 1, Name: Rank 1

TeamID: 1, Name: Rank 2

TeamID: 2, Name: Rank 1

TeamID: 1, Name: Rank 3

TeamID: 2, Name: Rank 2

 

Then something like this to get the list of ranks for a specific team: 

SELECT * FROM TeamRanks WHERE TeamID LIKE getPlayerTeamId(player)

 

Link to comment

@CodyL im working on a gang panel with custom ranks.But in order to generate those ranks with permissions i need to have several ranks within one column.So basically if you run the gangname by select and you try to get Ranks it should output something like this.Leader,Headquarter,Prospect. This is the column value of one single row

@pa3ck thank you thats what ive been  searching for didnt know toJSON would handle this and just use the fromJSON to get the actual data within a table.Ive managed to fix this already thanks to this. :D

Link to comment

New problem appeared, Somehow i can't get the data from the mysql server.I've a table in return but somehow i can't get it to splitup the JSON

 

function getConnection()
connection = dbConnect( "mysql", "dbname=fghousing;host=localhost", "root", "", "share=1" )
if ( connection ) then -- The connection failed
outputDebugString("Connected to the MySQL server")
json = toJSON ( {"Headquarter", "Leader", "Prospect"}, true, "none" )
dbExec(connection, "INSERT INTO newtest VALUES (?,?)", json, "Syntrax#")
outputDebugString("Inserted Rows")
else
outputDebugString("Unable to connect to the MySQL server")
end
end
addEventHandler("onResourceStart",resourceRoot,getConnection)

function getJSON()
connection = dbConnect( "mysql", "dbname=fghousing;host=localhost", "root", "", "share=1" )
if ( connection ) then -- The connection failed
local ranksString = ""
local result = dbQuery(connection, "SELECT * FROM newtest WHERE username=?", "Syntrax#") -- Execute the query
if result then
resultdata = dbPoll(result, -1)
if resultdata then
for i, v in ipairs(resultdata) do
value = unpack(v)
outputDebugString(fromJSON(value))
end
end
end
end
end
addCommandHandler("getJSON", getJSON)

 

Link to comment
  • Moderators

How does the JSON looks like?

How do you split up the JSON? (Note: if you use string keys in JSON, all numeric keys also become strings, which is very annoying)

How does the data looks like in the mysql database?

Tip:

else
	outputDebugString("Unable to connect to the MySQL server")
	cancelEvent() -- don't start a resource that doesn't have connection to a mySQL server! Else you get an enormous flow of errors/warnings if the connection fails.
end

 

Normally an unique id is the first in the column and the rest of the data comes after that.

 

 

Edited by IIYAMA
  • Like 1
Link to comment

You don't need to split up the JSON. Look at this example to see how it works

local jsonString = toJSON ( {"Headquarter", "Leader", "Prospect"}, true, "none" )
local jsonTable = fromJSON(jsonString) -- a LUA table from JSON string
outputDebugString(jsonTable[1]) --> "Headquarter"

 

Link to comment
18 hours ago, pa3ck said:

You don't need to split up the JSON. Look at this example to see how it works


local jsonString = toJSON ( {"Headquarter", "Leader", "Prospect"}, true, "none" )
local jsonTable = fromJSON(jsonString) -- a LUA table from JSON string
outputDebugString(jsonTable[1]) --> "Headquarter"

 

This works when having those above eachother.Now i have to find a method to get it back from MYSQL.The data has been stored as 

[["Headquarter","Leader","Prospect"]]  

 

But once i've done the query and Poll the results i've encountered a problem.It doesn't quite do anything.Tried 

For i, v in ipairs(resultdata) do
  outputDebugString(v) -- Outputs Table : random:~
  outputDebugString(fromJSON(v)) -- Outputs nil
  end

I've also tried

For i, v in ipairs(resultdata) do
  stringG = v
  for i, v in ipairs(stringG) do
    outputDebugstring(v[1])
    outputDebugstring(fromJSON(v)[1])
    end
  end

 

Link to comment
  • Moderators
outputDebugString("Debug info start: " .. getTickCount())
for i, v in ipairs(resultdata) do
	inspect ( i )
	inspect ( v )
	-- https://wiki.multitheftauto.com/wiki/Inspect
end
?

    outputDebugString("Debug info start: " .. getTickCount())
    for i, v in ipairs(resultdata) do
      outputDebugString("type(resultdata) =  " .. type(resultdata))
      if type(v) == "table" then
        for j, data in pairs(v) do -- pairs << very important
          inspect ( j )
          inspect ( data )
          -- https://wiki.multitheftauto.com/wiki/Inspect
        end
      else
        outputDebugString("V ~= table, but: " .. type(v))
      end
    end

I actually forgot how to do it too, haha. Well I do remember that there has to be also a pairs loop in it, because it uses columns as keys.

Edited by IIYAMA
  • Like 1
Link to comment

This is working fine for me:

function addTeam()

	local ranks = toJSON ( {"Headquarter", "Leader", "Prospect"}, true, "none" )
	dbExec(connection, "INSERT INTO teams (team_name, team_ranks) VALUES ( ?, ? )", "TestName", ranks )

end

function getRank()

	local qry = dbQuery(connection, "SELECT * FROM teams WHERE team_id LIKE 1 LIMIT 1")
	local res = dbPoll(qry, -1)
	local teamRank
	
	if(res and #res >0) then
	
		for k, rows in ipairs(res) do
		
			teamRank = fromJSON(rows["team_ranks"]) -- "team_ranks" is the column name in my table
		
		end
	
	end
	
	for i = 1, #teamRank do
		outputChatBox("Rank " .. i .. ": " .. teamRank[i])
	end

end

1bdfac57900b47ef83922c8f76cf6c5b.png

Edited by pa3ck
Link to comment
On 20-1-2017 at 11:50, pa3ck said:

This is working fine for me:


function addTeam()

	local ranks = toJSON ( {"Headquarter", "Leader", "Prospect"}, true, "none" )
	dbExec(connection, "INSERT INTO teams (team_name, team_ranks) VALUES ( ?, ? )", "TestName", ranks )

end

function getRank()

	local qry = dbQuery(connection, "SELECT * FROM teams WHERE team_id LIKE 1 LIMIT 1")
	local res = dbPoll(qry, -1)
	local teamRank
	
	if(res and #res >0) then
	
		for k, rows in ipairs(res) do
		
			teamRank = fromJSON(rows["team_ranks"]) -- "team_ranks" is the column name in my table
		
		end
	
	end
	
	for i = 1, #teamRank do
		outputChatBox("Rank " .. i .. ": " .. teamRank[i])
	end

end

1bdfac57900b47ef83922c8f76cf6c5b.png

Hmm thanks but using another method now which is more efficient

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