0

我正在尝试将一些数据保存到表中。我从数据库中获取数据,它工作正常。我的问题是数据没有保存在表中。它是一个类似于 table = {} 的 lua 表,而不是数据库表。

也许它已保存,但看起来打印是在保存之前完成的,即使我在之后调用它们。事实上,即使我先调用它,我的网络请求似乎也是在我的程序中最后完成的。

我真的很想知道这其中的原因。有任何想法吗?

这是代码:

---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!")

这是输出:

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

顺便说一句,navTable innehÃ¥ller 表示 navTable 包含。

4

1 回答 1

1

答案是 networklistener 与其余代码并行运行。

于 2013-05-29T08:40:16.740 回答