我正在使用数据库来保持高分记录。在情节提要场景更改上,我制作文件以显示高分。我先制作数据库,然后制作表格,然后检索记录。检查现在的得分是否超过数据库中的现有分数。更新第一条现有记录。
它在模拟器上工作得非常好,但在设备上它停留在前一个场景上并且永远不会将场景更改为高分文件。
在实施数据库之前,它正在改变场景。
local myData = require("myData")
-----------------------------------------------------------------------------
-- DATABASE
-- SQLite
-----------------------------------------------------------------------------
--Include sqlite
require "sqlite3"
local path = system.pathForFile("myDataBaseASDF.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
local tablesetup = [[CREATE TABLE IF NOT EXISTS highScoreClassic (id INTEGER PRIMARY KEY, Time, Taps);]]
print("DB Created")
db:exec( tablesetup )
local dbRows = 0
local dbTaps = 0
local dbTime = "00:00:00"
--print all the table contents
for row in db:nrows("SELECT * FROM highScoreClassic WHERE id=1;") do
dbRows = dbRows + 1
dbTaps = row.Taps
dbTime = row.Time
print("Row Taken")
end
function isHighscore()
if myData.currentScore > dbTime then
return true
elseif myData.currentScore == dbTime and myData.taps > dbTaps then
return true
elseif myData.currentScore == dbTime and myData.taps < dbTaps then
return false
else return false end
end
function saveToDataBase()
print("updating")
local q = [[UPDATE highScoreClassic SET Time=']]..myData.currentScore..[[', Taps=]]..myData.taps..[[ WHERE id=1;]]
db:exec( q )
end
function saveToDataBaseFirstTime()
print("inserting")
local tablefill =[[INSERT INTO highScoreClassic VALUES (NULL, ']]..myData.currentScore..[[',']]..myData.taps..[['); ]]
db:exec( tablefill )
end
if ( dbRows > 0 ) then
if ( isHighscore() ) then
print(dbTime, dbTaps)
myData.highScore = true
saveToDataBase()
else
myData.highScore = false
print("No HighScore")
end
else
saveToDataBaseFirstTime()
end
--setup the system listener to catch applicationExit
Runtime:addEventListener( "system", onSystemEvent )