we can get the path of the current directory using the following code.
|
1 |
local path = system.pathForFile( "Icon.png", system.ResourceDirectory ) |
let us make technology work for you
we can read the data from the file using file:read().
|
1 2 3 4 5 6 7 8 |
--Get the file from a document directory.If the file is on same directory of a main.lua Then no need to --specify system.DocumentsDirectory local path = system.pathForFile( "data.txt", system.DocumentsDirectory ) local file = io.open( path, "r" ) if file then local contents = file:read( "*a" ) print( "Contents of " .. path .. "\n" .. contents ) io.close( file ) end |
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
-- 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 = sqlite3.open( 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 = row.country..", "..row.capital.." " local t = display.newText(text, 50, 50 +(40 * count), native.systemFont, 30) t:setTextColor(255,255,255) count = count + 1 end |
