dennis Posted December 26, 2012 Share Posted December 26, 2012 heya all when I want to update an value in my database it needs to be done dynamically (by the user) for example. when the value in the database = 1, the user may want to change that to 0 (when he sets the Bloom off) I can do it hard coded by using: for test in db:nrows("UPDATE test SET bloom = 1 WHERE bloom = 0") I already have an placeholder for this which is local bloomval =1 How to do this so the value of bloomval = 1 can be set by the user ? Regards, Quote Link to comment Share on other sites More sharing options...
ChrisMAN Posted December 27, 2012 Share Posted December 27, 2012 I don't quite understand what you are asking. If i understand this is a straight forward way about it. local settings = db:nrows("Select * from settings")[1] or CreateSettings() -- singleton row function SetSetting(field, value) db:exec("UPDATE settings SET " .. field .. " = " value") settings[field] = value end Quote Link to comment Share on other sites More sharing options...
dennis Posted December 27, 2012 Author Share Posted December 27, 2012 okay thanks what I wanted to do is that I have sliders and check boxes, those will set an value. I want that value to be returned to the code and updated in the database... And the code fragment I showed will do that but I can not add my own value to it. Quote Link to comment Share on other sites More sharing options...
Rick Posted December 27, 2012 Share Posted December 27, 2012 A good number of sql engines have a concept they call prepared statements. This allows you to define parameters and set those parameters with values. The reason you want to use these instead of building the sql string yourself is to avoid something they call sql injection. Here is an example on how to do it using Lua: http://www.nessie.de/mroth/lua-sqlite3/documentation.html#ref11 . This is the preferred method. You also get a speed boost by using prepared statements. 1 Quote Link to comment Share on other sites More sharing options...
ChrisMAN Posted December 28, 2012 Share Posted December 28, 2012 If you want to hook into a setter for a table you will have to use a metatable __index/__newindex function. http://www.lua.org/pil/13.4.1.html 1 Quote Link to comment Share on other sites More sharing options...
dennis Posted December 28, 2012 Author Share Posted December 28, 2012 Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.