samedi 28 mars 2015

Reading updated records from sqlite in corona sdk in simulator

I've created db.sqlite inside resource direcotry of my project, I can read and everything is OK.


I've copied this database and pasted it to documents directory to make updates and everything is also OK.


But how can I read updated data from db.sqlite in the simulator? Do I have to copy the database from documents directory to resource direcotry again? I see this video the data is refreshed as well!


Here's my Code



local sqlite = require("sqlite3");
local path, db;
local dataBasePath = "./db.sqlite";

local function dbInit()
path = system.pathForFile(dataBasePath,system.DocumentsDirectory);
file = io.open(path,"r");
if(file==nil) then
local pathSource = system.pathForFile(dataBasePath,system.ResourceDirectory);
local fileSource = io.open(pathSource,"r");
local contentsSource = fileSource:read("*a");

local pathDestination = system.pathForFile(dataBasePath,system.DocumentsDirectory);
local fileDestination = io.open(pathDestination,"w");
fileDestination:write(contentsSource);

io.close(fileSource);
io.close(fileDestination);
end
print("yes");
end

dbInit();

-- SQL
function getData()
path = system.pathForFile("./db.sqlite",system.ResourceDirectory);
db = sqlite.open(path);
local sql = "SELECT * FROM options WHERE id = 1";
local options = {};
for row in db:nrows(sql) do
options = {scene=row.scene, language = row.language, music = row.music};
end
return options;
end

function updateData(name,value)
path = system.pathForFile(dataBasePath,system.DocumentsDirectory);
db = sqlite.open(path);
local sql = "UPDATE options SET "..name.."='"..value.."' WHERE id = 1";
db:exec(sql);
if db:errcode() then
print(db:errcode(), db:errmsg())
end
end

-- Events
local function onSystemEvent(e)
if(db and e.type == "applicationExit") then
db:close();
end
end

Runtime:addEventListener("system",onSystemEvent);

Aucun commentaire:

Enregistrer un commentaire