...all the things I tried, and the solution was so easy and obvious...
This is a guide which will (try to) explain how to connect to, and manipulate, an online MySQL database from your APMS application.
Here's what you need to do it yourself:
- APMS 6 (or 5 Pro)
- lua50.dll (get it here) (not lua 5.1, but 5.0)
- lua mysql binaries (get them here) (download only binares you need; tested with mysql5)
Place the following files from the downloaded stuff in your project's AutoPlay\Scripts folder:
- luasql\mysql.dll (This is a subfolder luasql)
- compat-5.1.lua
- libmySQL.dll
- lua50.dll
Put the following code in your Global functions:
Use this code to initiate a connection with the server and the database:Code:-- Load compat for Lua 5.1 package proposal compatibility require ("compat-5.1.lua"); package.cpath = _SourceFolder.."\\AutoPlay\\Scripts\\?.dll;" -- Set the working folder before loading the library -- so that mySQL can find all appropriate DLLs local strOldWorkingFolder = Folder.GetCurrent(); Folder.SetCurrent(_SourceFolder.."\\AutoPlay\\Scripts"); require"luasql.mysql" Folder.SetCurrent(strOldWorkingFolder);
(add your server details where appropiate)
Use this code to fetch data from, in this example, table test to a ListBox1 (our table has two columns: id and ime):Code:env = assert (luasql.mysql()) con = assert (env:connect("database_name","username","password","server_address","port"));
This "a" in the code above means to return alphanumerically indexed table.Code:ListBox.DeleteItem("ListBox1", -1); cur = assert (con:execute"SELECT id, ime FROM test") row = cur:fetch ({}, "a") while row do ListBox.AddItem("ListBox1", row.ime.."", row.id..""); row = cur:fetch (row, "a") end cur:close();
The same way I used SELECT in the upper example, you can use INSERT, UPDATE and all the other SQL commands supported by MySQL database. Additional info on usage and syntax can be found here: http://luaforge.net/projects/luasql
I would like to send a big thanks for helping me with this to:
- azmanar (pointing me to luaSQL)
- Brett (his guide about LuaSockets, and other things he coded for us)
- LuaSQL developers (obvious for what)
- rest of Lua and APMS commuity (for being so cool)

