HI
I have main.lua file ,now I would like to add this code in un
external file(database.lua)
--Include sqlite
require "sqlite3"
--Open data.db. If the file doesn't exist it will be created
local path = system.pathForFile("data.db", system.DocumentsDirectory)
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
--Setup the table if it doesn't exist
local tablesetup = [[CREATE TABLE IF NOT EXISTS points (id INTEGER PRIMARY KEY, player, point );]]
print(tablesetup)
db:exec( tablesetup )
function insertDBPoint()
--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
local testvalue = {}
testvalue[1] = 'erasmo'
testvalue[2] = '25'
local tablefill =[[INSERT INTO points VALUES (NULL, ']]..testvalue[1]..[[',']]..testvalue[2]..[[');]]
-- local tablefill2 =[[INSERT INTO points VALUES (NULL, ']]..testvalue[2]..[[',']]..testvalue[1]..[[');]]
db:exec( tablefill )
-- db:exec( tablefill2 )
end
--print the sqlite version to the terminal
print( "version " .. sqlite3.version() )
function selectRecord()
--print all the table contents
for row in db:nrows("SELECT \* FROM points") do
local text = row.player.." "..row.point
-- local t = display.newText(text, 20, 120 + (20 \* row.id), native.systemFont, 16)
-- t:setTextColor(255,0,255)
print( "text " ..text )
end
end
--setup the system listener to catch applicationExit
Runtime:addEventListener( "system", onSystemEvent )
I’d like to call these functions in main.lua:
function insertDBPoint()
--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
local testvalue = {}
testvalue[1] = 'erasmo'
testvalue[2] = '25'
local tablefill =[[INSERT INTO points VALUES (NULL, ']]..testvalue[1]..[[',']]..testvalue[2]..[[');]]
-- local tablefill2 =[[INSERT INTO points VALUES (NULL, ']]..testvalue[2]..[[',']]..testvalue[1]..[[');]]
db:exec( tablefill )
-- db:exec( tablefill2 )
end
--print the sqlite version to the terminal
print( "version " .. sqlite3.version() )
function selectRecord()
--print all the table contents
for row in db:nrows("SELECT \* FROM points") do
local text = row.player.." "..row.point
-- local t = display.newText(text, 20, 120 + (20 \* row.id), native.systemFont, 16)
-- t:setTextColor(255,0,255)
print( "text " ..text )
end
end
How I can call these functions?
Can I help me?
Thanks
[import]uid: 100428 topic_id: 22467 reply_id: 322467[/import]