Acessing SQLite Database in Corona

I’ll explain the basic steps to access SQLite database and loop through records in a table using Corona sdk.

Create sqlite database. (here I created country_capital.sqlite.)
Create table tbl_country_capital with the columns country and capital.

Copy the code below, paste it in main.lua file and execute it.. You’ll be able to see your table contents

-- inlcude sqlite db

require "sqlite3"

-- set the database path, here its the same folder where main.lua file resides.

local path = system.pathForFile("country_capital.sqlite", system.ResourceDirectory)

-- open db
db = path )
-- a variable to dynamically change the Y coordinate

local count = 1
-- sql query
local sql = "SELECT * FROM tbl_country_capital"
-- looping through result set and printing the output.

for row in db:nrows(sql) do

local text =", "" "
local t = display.newText(text, 50, 50 +(40 * count), native.systemFont, 30)
count = count + 1