Director Class with sqlite

Hello all, I’m playing with the director class and I find it very useful (thank you Ricardo) however I am trying to implement a button on the screen2 that will do a simple SQL query when pressed and display the results on screen and I can’t seem to figure out how to do it. Is anyone able to help me fill in the blanks? The sqlite samples don’t show how to query when a button is pressed. I’m really new at this and any help is appreciated. Thanks a lot!
[import]uid: 31262 topic_id: 6393 reply_id: 306393[/import]

As kind of a broad strokes look at it, you need to set up an EventListener for the button. That’s a function that will be run when the button is pressed. Inside that function is where you put the SQL query.

I’m just starting to play around with the SQLite data access in Corona. If I end up with some sample code I’ll be sure and post it.

Jay [import]uid: 9440 topic_id: 6393 reply_id: 22114[/import]

Thanks for the guidance J.A. I’ll play around with some of the samples given and see if I can figure it out. Of course if you do first, code would be most appreciated. Thanks again. [import]uid: 31262 topic_id: 6393 reply_id: 22166[/import]

OK so I’ve been playing around for a long time with the code and the SQLITE sample and I just can’t find what I need.

Can someone show me a sample of how to simply query a database and then display the results using display.newText ? A very simple query such as ‘select * from table1’ and then show the results on screen.

I think I can make progress from there. Thanks for anyone that can help. [import]uid: 31262 topic_id: 6393 reply_id: 22200[/import]

Here’s a self-contained project that pulls a single piece of data from a database and displays it. The only thing missing here is a database called agm.sqlite that has a column named gamename. That database file should live right in the main project folder along with main.lua, etc.

--Include sqlite  
require "sqlite3"  
local path = system.pathForFile("agm.sqlite", system.ResourceDirectory)  
db = sqlite3.open( path )   
   
--Handle the applicationExit event to close the db  
local function onSystemEvent( event )  
 if( event.type == "applicationExit" ) then   
 db:close()  
 end  
end  
  
function getGameInfo(part)  
 local output  
 local sql = "SELECT " .. part .. " FROM gameinfo limit 1"  
 for a in db:nrows(sql)  
 do  
 output = a[part]  
 end  
 return output  
end  
  
local text = display.newText(getGameInfo("gamename"), 60, 16)  
text.x = 100  
text.y = 100  
  
--setup the system listener to catch applicationExit  
Runtime:addEventListener( "system", onSystemEvent )  

Notice that I created a function called getGameInfo. I can pass into that any of the column names and it will pass me back the info, such as:

gameName = getGameInfo("gamename")  
gameDescription = getGameInfo("synopsis")  
gameAuthor = getGameInfo("author")  

I hope that helps.

Jay [import]uid: 9440 topic_id: 6393 reply_id: 22215[/import]

Jay, absolutely brilliant my friend. I made a few changes like the database name and column names, pointed it to system.DocumentsDirectory and I put it in its own .lua file and it works perfectly. Thanks again, I’ll start digging further into more complicated examples but there really isn’t enough documentation on how to use sqlite with Corona. Thanks again. [import]uid: 31262 topic_id: 6393 reply_id: 22229[/import]

Cool, glad it was useful to you!

I’m just starting work on my Adventure Game Machine which is all database-driven, so I’ll be diving deep into Corona+SQLite over the next few weeks. I’ll probably be posting more sample code on my http://GameDevNation.com site as I come up with it.

Jay [import]uid: 9440 topic_id: 6393 reply_id: 22240[/import]

Nice shameless plug lol. I signed up to your website and bookmarked it :slight_smile: [import]uid: 31262 topic_id: 6393 reply_id: 22244[/import]

I gotta thank the people in this thread (Especially J. A. Whye
!) for finally, FINALLY solving the issue of where the hell to put the actual db file to make the app work once it’s deployed.

MAN! I was having such issues! The whole “throw it in the scratch area, until you deploy…then just GUESS at where it should go” aspect was killing me!!

:slight_smile: [import]uid: 11636 topic_id: 6393 reply_id: 30001[/import]