How to call a function that has it's name in a sqllite table column?

I have a table named STATIONS. In that table I have a column that contains function names. I am selecting a function name and trying to execute it. Below is my snippet of code:

            require “sqlite3”
            local path = system.pathForFile(“trains.db”, system.DocumentsDirectory)
            db = sqlite3.open( path )
            local train1SQL = "SELECT T_STATIONS.FILE FROM T_STATIONS

            WHERE T_STATIONS.NAME = ‘Train1’

        
            for row in db:nrows(train1SQL) do           
                local runThisTrain = row.FILE

                   runThisTrain()

            end

row.File contains the function name that I am returning from the select statement. “runThisTrain” produces a Corona runtime error saying that I am attempting to call a string value.

I believe this would be helpful to you.

http://stackoverflow.com/questions/1791234/lua-call-function-from-a-string-with-function-name

Thanks, I actually saw that thread. However, I believe that loadstring() is not supported by Corona due to Apple restrictions.

I don’t see another way of doing this without loadstring(). Maybe Rob can enlighten me :slight_smile: .

I only tried for the global namespace, but the usage suggested in that thread works for me with Corona, e.g.

foo = function() print("bar") end local x = "foo" \_G[x]()

prints “bar”. No use of loadstring() involved

Would not the use of a sorter function using a simple if … elseif scenario work.

sorterfunction( runThisTrain )

if runThisTrain == “someString” then

    someString()

elseif runThisTrain == “otherString” then

    otherString()

end

type of thing.

T.

There was a blog tutorial post by Brent ( i think - maybe 2-3 mths ago ) which did something similar to this with timers - “pause”, “resume”, “start” strings

Brent’s tut on spawning

also Rob did a tut on variable argument functions 2 weeks later (end of Nov).

T.

 for row in db:nrows(train1SQL) do           
      local runThisTrain = row.FILE

     _GrunThisTrain 

end

 

Is this syntax correct? If so, it is not working.

I would skip the global and use a table instead. Example:

[lua]

local runTrains = {}

runTrains[“myTrain1”] = function() 

    print(“Running myTrain 1”)

end

local theTrainFromDB = “myTrain1”

runTrainstheTrainFromDB

[/lua]

My actual function is defined in a lua file. All that I am storing in the sqllite table is the function name.

My table column contains around 50 function names. When a certain event occurs I do a select from the table to get the function name and then I want to call that function.

 local sql = "SELECT T_STATIONS.FILE FROM T_STATIONS WHERE T_STATIONS.NAME = ‘Train1’

 for row in db:nrows(sql) do           
       local runThisFunction = row.FILE

       runThisFunction()

end

I think my example does what you want. It is calling a function from a string, which in your case will be gotten from a db instead. It had an error though, fixed now. 

That being said I don’t see syntax error with your way (using _G) but your function must be a global which is not good practice,:

[lua]

function myTrain1() – Can not be local

    print(“Running myTrain 1”)

end

local theTrainFromDB = “myTrain1”

_GtheTrainFromDB[/lua]

I believe this would be helpful to you.

http://stackoverflow.com/questions/1791234/lua-call-function-from-a-string-with-function-name

Thanks, I actually saw that thread. However, I believe that loadstring() is not supported by Corona due to Apple restrictions.

I don’t see another way of doing this without loadstring(). Maybe Rob can enlighten me :slight_smile: .

I only tried for the global namespace, but the usage suggested in that thread works for me with Corona, e.g.

foo = function() print("bar") end local x = "foo" \_G[x]()

prints “bar”. No use of loadstring() involved

Would not the use of a sorter function using a simple if … elseif scenario work.

sorterfunction( runThisTrain )

if runThisTrain == “someString” then

    someString()

elseif runThisTrain == “otherString” then

    otherString()

end

type of thing.

T.

There was a blog tutorial post by Brent ( i think - maybe 2-3 mths ago ) which did something similar to this with timers - “pause”, “resume”, “start” strings

Brent’s tut on spawning

also Rob did a tut on variable argument functions 2 weeks later (end of Nov).

T.

 for row in db:nrows(train1SQL) do           
      local runThisTrain = row.FILE

     _GrunThisTrain 

end

 

Is this syntax correct? If so, it is not working.

I would skip the global and use a table instead. Example:

[lua]

local runTrains = {}

runTrains[“myTrain1”] = function() 

    print(“Running myTrain 1”)

end

local theTrainFromDB = “myTrain1”

runTrainstheTrainFromDB

[/lua]

My actual function is defined in a lua file. All that I am storing in the sqllite table is the function name.

My table column contains around 50 function names. When a certain event occurs I do a select from the table to get the function name and then I want to call that function.

 local sql = "SELECT T_STATIONS.FILE FROM T_STATIONS WHERE T_STATIONS.NAME = ‘Train1’

 for row in db:nrows(sql) do           
       local runThisFunction = row.FILE

       runThisFunction()

end

I think my example does what you want. It is calling a function from a string, which in your case will be gotten from a db instead. It had an error though, fixed now. 

That being said I don’t see syntax error with your way (using _G) but your function must be a global which is not good practice,:

[lua]

function myTrain1() – Can not be local

    print(“Running myTrain 1”)

end

local theTrainFromDB = “myTrain1”

_GtheTrainFromDB[/lua]