Advertisement
Guest User

Untitled

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