0

I'm trying to save some data in to a table. I get the data from a database and it works ok. My problem is that the data is not saved in the table. It is a lua table like table = {} and NOT a database table.

Maybe it is saved but it looks like the prints are done before the saving even though I call them after. In fact it seems like my network request is done last in my program even though I call it first.

I would real like to know the reason for this. Any ideas?

Here is the code:

---TESTING!

print("Begin teting!")

--hej = require ( "test2" )

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()
    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

local function networkListener( event )
    if (event.isError) then
            print("Network error!")
    else
            myNewData = event.response
            print("From server: "..myNewData)
            decodedData = (json.decode(myNewData))
    SaveData2()
    --db:exec("DROP TABLE IN EXISTS navaltable")
    end
end

--function uppdateNavalTable()
    network.request( "http://127.0.0.1/firstMidle.php", "GET", networkListener )

--end
changeTab()
printNavTable()
--uppdateNavalTable()
printNavTable()

print("Done!")

And here is the output:

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:
2
0
navTable innehåller:
2
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: 008B5018
30
0
30
13

And btw, navTable innehåller means navTable contains.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Dave
  • 341
  • 1
  • 4
  • 13
  • Please, atleast why are the prints in wrong order. Are not programs suposed to run sequentially? – Dave Mar 21 '13 at 17:36

1 Answers1

1

The answer is that networklistener run parallell with the rest of the code.

Dave
  • 341
  • 1
  • 4
  • 13