Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1.  
  2. -- Add onscreen text
  3. local label1 = display.newText( "SQLite", 20, 30, native.systemFontBold, 20 )
  4. label1:setTextColor( 190, 190, 255 )
  5. local label2 = display.newText( "Creates or opens a local database", 20, 50, native.systemFont, 14 )
  6. label2:setTextColor( 190, 190, 255 )
  7. local label3 = display.newText( "(Data is shown below)", 20, 90, native.systemFont, 14 )
  8. label3:setTextColor( 255, 255, 190 )
  9.  
  10. --Include sqlite
  11. require "sqlite3"
  12. --Open data.db. If the file doesn't exist it will be created
  13. local path = system.pathForFile("data.db", system.DocumentsDirectory)
  14. db = sqlite3.open( path )
  15.  
  16. --Handle the applicationExit event to close the db
  17. local function onSystemEvent( event )
  18. if( event.type == "applicationExit" ) then
  19. db:close()
  20. end
  21. end
  22.  
  23. --Setup the table if it doesn't exist
  24. local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content, content2);]]
  25. print(tablesetup)
  26. db:exec( tablesetup )
  27.  
  28. --Add rows with a auto index in 'id'. You don't need to specify a set of values because we're populating all of them
  29. local testvalue = {}
  30. testvalue[1] = 'Hello'
  31. testvalue[2] = 'World'
  32. testvalue[3] = 'Lua'
  33. testvalue[4] = 'Tyler is'
  34. testvalue[5] = 'cool'
  35. --local tablefill =[[INSERT INTO test VALUES (NULL, ']]..testvalue[1]..[[',']]..testvalue[2]..[['); ]]
  36. --local tablefill2 =[[INSERT INTO test VALUES (NULL, ']]..testvalue[2]..[[',']]..testvalue[1]..[['); ]]
  37. --local tablefill3 =[[INSERT INTO test VALUES (NULL, ']]..testvalue[1]..[[',']]..testvalue[3]..[['); ]]
  38. local tablefill4 =[[INSERT INTO test VALUES (NULL, ']]..testvalue[4]..[[',']]..testvalue[5]..[['); ]]
  39. --db:exec( tablefill )
  40. --db:exec( tablefill2 )
  41. --db:exec( tablefill3 )
  42. db:exec( tablefill4 )
  43.  
  44. --print the sqlite version to the terminal
  45. print( "SQL Version " .. sqlite3.version() )
  46.  
  47. --print all the table contents
  48. for row in db:nrows("SELECT * FROM test") do
  49. local text = row.content.." "..row.content2
  50. local t = display.newText(text, 20, 120 + (20 * row.id), native.systemFont, 16)
  51. t:setTextColor(255,0,255)
  52. end
  53.  
  54. --setup the system listener to catch applicationExit
  55. Runtime:addEventListener( "system", onSystemEvent )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement