How can I read a SQL table?

Hello,

I have created a SQL table, with some fields (similar than a glossary, with ID, Term, Definition fields) with phpMyAdmin and some entries. I would like to create a simple app example, that allows integrate this table and also can read entries of my table. In UI will must appear an inputbox and users when write some words, can search if exists in my table.

I need that this table will be saved inside app (for users don’t need to use Internet). How can I do it?

Is there any example or documentation taht allow me to start this project?

Many thanks for your help! [import]uid: 173743 topic_id: 31618 reply_id: 331618[/import]

I used this code to create a table in the documents directory and acces the data. Works fine for me. :slight_smile:
If you need to use an existing table I think you can just add it to you project directory but then you only have read-access to the file. If you need to update the data you need to copy the table to the documents directory. I don’t know how to do that but the code below should work for creating a new table and access the records in the table.

require "sqlite3"  
  
local path = system.pathForFile( "data.db", system.DocumentsDirectory )  
local db = sqlite3.open( path )  
  
local tablesetup = [[CREATE TABLE IF NOT EXISTS events (id INTEGER PRIMARY KEY autoincrement, titel, year, month, day, hh, mm, link);]]  
db:exec( tablesetup )  
  
-- Close database on exit  
local function onSystemEvent( event )  
 if event.type == "applicationExit" then  
 if db and db:isopen() then  
 db:close()  
 end  
 end  
end  
Runtime:addEventListener( "system", onSystemEvent )  
  
-- Then read the data  
local sEvents = {} -- starts off emtpy  
  
for row in db:nrows("SELECT \* FROM events ORDER BY year, month, day")   
  
 -- Fill events  
 sEvents[#sEvents+1] =  
 {   
 order = #sEvents+1,  
 id = row.id,  
 titel = row.titel,  
 year = row.year,  
 month = row.month,  
 day = row.day,  
 hh = row.hh,  
 mm = row.mm,  
 link = row.link  
 }  
  
end  

You can also check this blogpost: http://www.coronalabs.com/blog/2012/04/03/tutorial-database-access-in-corona/ [import]uid: 98060 topic_id: 31618 reply_id: 126289[/import]

Edit: Looks like @mfr has already responded with a very solid answer, but I suppose it won’t hurt to leave my post as well. Cheers. Naomi


@gturmo, I’m not sure what you mean by “I need that this table will be saved inside app (for users don’t need to use Internet)”

If you intend to pull out data from a SQL table residing on a server, what you need to use is the network.request and here are some helpful posts that I looked at when I started working on my app to send/fetch data to/from mySQL via PHP:

http://developer.coronalabs.com/forum/2012/04/16/tutorial-how-make-your-app-talk-web-service-or-how-setup-web-service-your-app-talk
http://docs.coronalabs.com/api/library/network/index.html
http://developer.coronalabs.com/forum/2012/04/03/networkrequest

If you want to setup database inside your app (and not talk to server), then you may want to look into sqlite3: http://docs.coronalabs.com/api/library/sqlite3/index.html

As for saving lua table, you might want to look into this super straight forward & slick save-load functions in code share:
https://developer.coronalabs.com/code/super-simple-lua-table-save-and-load-file-functions

I hope this helps.

Naomi [import]uid: 67217 topic_id: 31618 reply_id: 126292[/import]

@Naomi and @mfr: Many thanks for your support! I think that I need a SQL lite, because now I have an external SQL table, that I want * SELECT trought my app. I’m studying this tutorial that I think can help me: http://www.coronalabs.com/blog/2012/04/03/tutorial-database-access-in-corona/ but it still many questions that I don’t know.

I saw that first of all I need to declare to use SQlite functions:

[lua]require “sqlite3”

local path = system.pathForFile( “data.db”, system.DocumentsDirectory )
local db = sqlite3.open( path )[/lua]

1.- I don’t understand this because, my file is called data.sql and not, data.db.
2.- Must I save my data.sql file in root of my project or must I create an “DocumentsDirectory” folder?
3.-

Also, I’m readin this tutorial: http://zetcode.com/databases/sqlitetutorial/select/ to learn how can I do calls with * SELECT to do searches inside my table.

At the end of this thread I would like to create a little example file, to use an external SQL table and work with this.

Many thanks! [import]uid: 173743 topic_id: 31618 reply_id: 126391[/import]

I used this code to create a table in the documents directory and acces the data. Works fine for me. :slight_smile:
If you need to use an existing table I think you can just add it to you project directory but then you only have read-access to the file. If you need to update the data you need to copy the table to the documents directory. I don’t know how to do that but the code below should work for creating a new table and access the records in the table.

require "sqlite3"  
  
local path = system.pathForFile( "data.db", system.DocumentsDirectory )  
local db = sqlite3.open( path )  
  
local tablesetup = [[CREATE TABLE IF NOT EXISTS events (id INTEGER PRIMARY KEY autoincrement, titel, year, month, day, hh, mm, link);]]  
db:exec( tablesetup )  
  
-- Close database on exit  
local function onSystemEvent( event )  
 if event.type == "applicationExit" then  
 if db and db:isopen() then  
 db:close()  
 end  
 end  
end  
Runtime:addEventListener( "system", onSystemEvent )  
  
-- Then read the data  
local sEvents = {} -- starts off emtpy  
  
for row in db:nrows("SELECT \* FROM events ORDER BY year, month, day")   
  
 -- Fill events  
 sEvents[#sEvents+1] =  
 {   
 order = #sEvents+1,  
 id = row.id,  
 titel = row.titel,  
 year = row.year,  
 month = row.month,  
 day = row.day,  
 hh = row.hh,  
 mm = row.mm,  
 link = row.link  
 }  
  
end  

You can also check this blogpost: http://www.coronalabs.com/blog/2012/04/03/tutorial-database-access-in-corona/ [import]uid: 98060 topic_id: 31618 reply_id: 126289[/import]

Edit: Looks like @mfr has already responded with a very solid answer, but I suppose it won’t hurt to leave my post as well. Cheers. Naomi


@gturmo, I’m not sure what you mean by “I need that this table will be saved inside app (for users don’t need to use Internet)”

If you intend to pull out data from a SQL table residing on a server, what you need to use is the network.request and here are some helpful posts that I looked at when I started working on my app to send/fetch data to/from mySQL via PHP:

http://developer.coronalabs.com/forum/2012/04/16/tutorial-how-make-your-app-talk-web-service-or-how-setup-web-service-your-app-talk
http://docs.coronalabs.com/api/library/network/index.html
http://developer.coronalabs.com/forum/2012/04/03/networkrequest

If you want to setup database inside your app (and not talk to server), then you may want to look into sqlite3: http://docs.coronalabs.com/api/library/sqlite3/index.html

As for saving lua table, you might want to look into this super straight forward & slick save-load functions in code share:
https://developer.coronalabs.com/code/super-simple-lua-table-save-and-load-file-functions

I hope this helps.

Naomi [import]uid: 67217 topic_id: 31618 reply_id: 126292[/import]

@Naomi and @mfr: Many thanks for your support! I think that I need a SQL lite, because now I have an external SQL table, that I want * SELECT trought my app. I’m studying this tutorial that I think can help me: http://www.coronalabs.com/blog/2012/04/03/tutorial-database-access-in-corona/ but it still many questions that I don’t know.

I saw that first of all I need to declare to use SQlite functions:

[lua]require “sqlite3”

local path = system.pathForFile( “data.db”, system.DocumentsDirectory )
local db = sqlite3.open( path )[/lua]

1.- I don’t understand this because, my file is called data.sql and not, data.db.
2.- Must I save my data.sql file in root of my project or must I create an “DocumentsDirectory” folder?
3.-

Also, I’m readin this tutorial: http://zetcode.com/databases/sqlitetutorial/select/ to learn how can I do calls with * SELECT to do searches inside my table.

At the end of this thread I would like to create a little example file, to use an external SQL table and work with this.

Many thanks! [import]uid: 173743 topic_id: 31618 reply_id: 126391[/import]