Hello!
I’m trying to make a program that get data from a database very often. Now I got this strange problem. I have a networklistener that is used in a network request. First I managed the data I got with the networklistener in another function that was called in the networkrequest but after severel hours (days!) of trial and error I put everything in the networklistener just to see if it worked.
I alse added a local variable outside of the networkrequest-function that I changed last in the networkrequest function to see if atleast that one got changed to true but it wasn’t! All variables I use are only active in the function and not out side it.
I’m going to explain as good as I can what I’m trying to achive.
-
Get data from database
-
Store it in a lua table (it’s like 20 values)
-Use the table in my application (One of them is just a speedometre like in a car)
I use a three-tier architecture so the connection to the database and the querries to it is done on another server.
All my app is doing is pulling that answer from the server via a JSON object so the variable “myNewData” contains the JSON object and the variable “decodedData” contains the… decoded JSON object.
My real problem is that the data is not saved outside the networklistener. Its gone.
Can anyone see what I’m doing wrong. I’m sorry for my bad looking code, but I’ve tried to solve the same problem for days and it starts to get messy…
This is the code.
print("Begin teting!") done1 = false local navTable = { Eng\_Spd = 0, Spd\_Set = 0 } local changeTab = function() navTable.Eng\_Spd = 2 end printNavTable = function() print("navTable innehåller: ") print(navTable.Eng\_Spd) print(navTable.Spd\_Set) end require "sqlite3" local myNewData local json = require ("json") local decodedData local SaveData2 = function(tab) local i = 1 local counter = 1 local index = "livedata"..counter local navValue = decodedData[index] print(navValue) while (navValue ~=nil) do --tablefill ="INSERT INTO navaltable VALUES (NULL,'" .. navValue[1] .. "','" .. navValue[3] .."','" .. navValue[4] .."','" .. navValue[5] .."','" .. navValue[6] .."');" --print(tablefill) --db:exec(tablefill) if navValue[3] == "Eng Spd" then navTable.Eng\_Spd = navValue[4] elseif navValue[3] == "Spd Set" then navTable.Spd\_Set = navValue[4] else print("blah") end print(navTable.Eng\_Spd) print(navTable.Spd\_Set) counter=counter+1 index = "livedata"..counter navValue = decodedData[index] end end function networkListener( event ) if (event.isError) then print("Network error!") else myNewData = event.response print("From server: "..myNewData) decodedData = (json.decode(myNewData)) local counter = 1 local index = "livedata"..counter local navValue = decodedData[index] print(navValue) while (navValue ~=nil) do --tablefill ="INSERT INTO navaltable VALUES (NULL,'" .. navValue[1] .. "','" .. navValue[3] .."','" .. navValue[4] .."','" .. navValue[5] .."','" .. navValue[6] .."');" --print(tablefill) --db:exec(tablefill) if navValue[3] == "Eng Spd" then navTable.Eng\_Spd = navValue[4] print("FÖR FAAAAAAAAAAAAAN!") elseif navValue[3] == "Spd Set" then navTable.Spd\_Set = navValue[4] else print("blah") end print(navTable.Eng\_Spd) print(navTable.Spd\_Set) counter=counter+1 index = "livedata"..counter navValue = decodedData[index] end --db:exec("DROP TABLE IN EXISTS navaltable") end print("Set done 1 to true") done1 = true end printNavTable() network.request( "http://127.0.0.1/firstMidle.php", "GET", networkListener ) if done1 == true then --changeTab() print("in done!") --uppdateNavalTable() printNavTable() end print("Done!")
And here is the output (console):
Copyright (C) 2009-2012 C o r o n a L a b s I n c . Version: 2.0.0 Build: 2012.971 Begin teting! navTable innehåller: 0 0 Done! From server: {"livedata1":["1","0","Eng Spd","30","0","2013-03-15 11:35:48"],"li vedata2":["1","1","Spd Set","13","0","2013-03-15 11:35:37"]} table: 02EF1330 FÖR FAAAAAAAAAAAAAN! 30 0 30 13 Set done 1 to true
And finaly, sorry for this wall of text but I realt need help here and I want you to understand my problem.
Kind regards!