Skip to content

Instantly share code, notes, and snippets.

@cjanis
Created November 28, 2012 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjanis/4159408 to your computer and use it in GitHub Desktop.
Save cjanis/4159408 to your computer and use it in GitHub Desktop.
Data Storage with Corona SDK
--os.remove(system.pathForFile( "data.db", system.DocumentsDirectory))
-- read data
function readData()
local path = system.pathForFile("data.db", system.DocumentsDirectory)
local file = io.open(path, "r")
if (file) then
data = json.decode(file:read("*a"))
else
file = io.open(path, "w")
file:write("{ \"visits\": 0, \"rated\": \"no\", \"iap\": { \"iapBalloons\": \"no\" } }")
data = { visits = 0, rated = "no", iap = { iapBalloons = "no" } }
end
io.close(file)
end
readData()
-- write data
function writeData()
os.remove(system.pathForFile( "data.db", system.DocumentsDirectory))
local path = system.pathForFile("data.db", system.DocumentsDirectory)
local file = io.open(path, "w")
file:write("{ \"visits\": ", data.visits ,", \"rated\": \"", data.rated ,"\", \"iap\": { \"iapBalloons\": \"", data.iap.iapBalloons ,"\" } }")
io.close(file)
readData()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment