Simple Example of Connecting to MySQL database

Hi,

I’m a newbie to both Lua and Corona SDK. I’ve been building a Java application that interacts with a MySQL database. Well as it turns out, I may have to move to the mobile platform arena. Therefore, I’ve been trying to get familiar with Lua programming and the Corona SDK environment. I like to learn by example so I was wondering if there is a simple example of a program that connects to and lists the rows of a table in a MySQL database from within the Corona SDK environment.

I can run the following program from the command line and it works. If I try from within a Corona SDK project, it doesn’t work (it can’t find the luasql.mysql module):

require "luasql.mysql";  
env = assert((luasql.mysql()), "Uh oh, couldn't load driver")  
conn = assert(env:connect("database","username","password","localhost"), "Oops!!")  
cur = assert (conn:execute ("SELECT \* from table\_1" ))  
row = cur:fetch ({}, "a")  
while row do  
 print ("\n------ new row ---------\n")  
 table.foreach (row, print)  
 row = cur:fetch (row, "a")  
end  
cur:close()  
conn:close()  
env:close()  

Particulars:
I downloaded and install Lua from luaforwindows (http://code.google.com/p/luaforwindows/)
I running on a Windows 7 machine.

Thanks times infinity for any help/suggestions
Bill
[import]uid: 173103 topic_id: 30562 reply_id: 330562[/import]

Either you need to find the luasql.mysql.lua file (which I suspect is actually a compiled C library instead of a lua file) and put it in your project or convince Corona Labs to include it with their product… or get Corona Enterprise so you can build in your own external libraries.

However, I would recommend that you not directly connect to your database. It’s a security issue. You should have some middleware in PHP or ASP or some other server scripting language that your app talks to that makes managed calls to your database.

Anyone with a packet sniffer can watch your traffic from your app and get the login credentials to your mysql server which has to be sitting on an open port for this to work anyway. [import]uid: 19626 topic_id: 30562 reply_id: 122455[/import]

I forgot to add that I am using the trial version of Corona SDK. If this trial version won’t support external libraries, then this exercise is at a dead end until I can upgrade. (BTW: The only file I can find is the mysql.dll, which resides in the …\lua\5.1\clibs\luasql directory.)

Based on your security comment, if it’s best not to connect directly to the database from the mobile app, then I don’t need to worry about this issue anyway :slight_smile:

Thanks!! [import]uid: 173103 topic_id: 30562 reply_id: 122476[/import]

Either you need to find the luasql.mysql.lua file (which I suspect is actually a compiled C library instead of a lua file) and put it in your project or convince Corona Labs to include it with their product… or get Corona Enterprise so you can build in your own external libraries.

However, I would recommend that you not directly connect to your database. It’s a security issue. You should have some middleware in PHP or ASP or some other server scripting language that your app talks to that makes managed calls to your database.

Anyone with a packet sniffer can watch your traffic from your app and get the login credentials to your mysql server which has to be sitting on an open port for this to work anyway. [import]uid: 19626 topic_id: 30562 reply_id: 122455[/import]

I forgot to add that I am using the trial version of Corona SDK. If this trial version won’t support external libraries, then this exercise is at a dead end until I can upgrade. (BTW: The only file I can find is the mysql.dll, which resides in the …\lua\5.1\clibs\luasql directory.)

Based on your security comment, if it’s best not to connect directly to the database from the mobile app, then I don’t need to worry about this issue anyway :slight_smile:

Thanks!! [import]uid: 173103 topic_id: 30562 reply_id: 122476[/import]