Checking if an item in a database exists

It’s kind of a roll-your-own type of implementation. I’d suggest checking out Rob’s tutorial on .json. If I could figure it and use it, anyone can:

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

A wonderful young lady by the name of Peach Pellen used to be a forum super-moderator, but she has since moved onto greener pastures. She created a bunch of nifty libraries for Corona, but they are mostly lost in the ether of the internet. I have a few, and one of the ones I salvaged was her Ego Level selection library. This illustrates how one would handle saving, loading and referencing data. it can apply to high scores, levels, unlockables, whatever. 

Let me know how you get on. Good Luck!

https://www.dropbox.com/s/l3cezaxw6gkf7xn/ego_levels.zip

Well, it doesn’t make sense, really.  Whenever I try to load a Json table at the beginning, it says the table is a nil table. IF you want to keep trying to help, you can, but you don’t have too. Thanks for your help!

I don’t mind helping. I’m no expert by any stretch, but helping others helps everyone in the long run, so I’m happy to lend a hand.

As for you getting another nil error when using json strings, I’m going to assume it’s still not implemented correctly. Here’s basically what I have for json game data:

--main.lua local json = require("json") local pathOne = system.pathForFile( "SavedGameOneSettings.json", system.DocumentsDirectory ) local oneFile = io.open( pathOne ) local pathGame = system.pathForFile( "GameSettings.json", system.DocumentsDirectory ) local gameFile = io.open( pathGame ) function saveTableOne(t, filename) local path = system.pathForFile( "SavedGameOneSettings.json", system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) print("File Encoded!") return true else return false end end function saveTableGame(t, filename) local path = system.pathForFile( "GameSettings.json", system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) print("File Encoded!") return true else return false end end function loadTableGame(filename) local path = system.pathForFile( "GameSettings.json", system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end function loadTableOne(filename) local path = system.pathForFile( "SavedGameOneSettings.json", system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end --[[Create your GameSettings and SavedGameOneSettings variables here]] if oneFile then print( "File exists" ) oneFile.close(oneFile) else saveTableOne(SavedGameOneSettings, "SavedGameOneSettings.json") print( "SavedGameOneSettings didn't exist, but now IT DOES!" ) end if gameFile then print( "File exists" ) gameFile.close(gameFile) else saveTableGame(GameSettings, "GameSettings.json") print( "GameSettings didn't exist, but now IT DOES!" ) end

 free to copy it at will. I think I just adapted what Rob described in his tutorial. Put this in your main.lua and it will tell you pretty clearly in your terminal if the table exists or not. If you wanted to do something like testvalue[1] and adapt it to this snippet, you could do something like this:

--[[Create your GameSettings and SavedGameOneSettings variables here]] SavedGameOneSettings.testvalue= { 0,0,0,0,0}

The above creates tables somewhat similar to what you were doing. The first code snippet will create the second code snippet and will check to see if it’s created. Now, you can have a function that does this:

local function checkDataTest1() if SavedGameOneSettings.testvalue[1] == 0 then print("JSON STRING SAVE AND LOAD SUCCESS!") end end checkDataTest1()

Try that out and let me know how it works; you should be able to convert the important parts to your needs.

Wow! Works like a charm. The “table exists” works perfectly! I am probably getting a little annoying. How do you save and load stuff using Json? I really don’t want to use mySql lite.

How do you save and load stuff using Json?

It’s as easy as 1-2-3 with the code above. If you take a look at this piece:

saveTableOne(SavedGameOneSettings, "SavedGameOneSettings.json")

That will save your SavedGameOneSettings variables whenever you change them. So, let’s say we do this:

 local function increaseScore() SavedGameOneSettings.testvalue[1] = SavedGameOneSettings.testvalue[1]+10 SavedGameOneSettings.testvalue[2] = SavedGameOneSettings.testvalue[2]+20 saveTableOne(SavedGameOneSettings, "SavedGameOneSettings.json") end

Then, every time you run the increaseScore() function, it increases the score by the given amounts, and saves the entire .json string. To make sure it works for sure, click on File in your Corona Simulator, click on Show Project Sandbox, and click on Documents. This should have all of your json files saved.

Loading files is pretty much the same thing:

SavedGameOneSettings = savemanage.loadTableOne("SavedGameOneSettings.json")

Not as many uses, but still necessary.

The above code I provided isn’t optimized as the functions are running as globals, so you’re going to want to do a bit more reading about external modules (http://www.coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/) in order to refine the code. 

Hope this helps! 

Thanks so far! I have one more question and I Think I will be set. How do you save testvalue[1] directly to SavedGameOneSettings?

I tried this:

[lua]

[[

–testvalue stuff

]]

[/lua]

But it didn’t work. I got a syntax error.

 So I just need to know how to save testvalue[1] directly to SavedGameOneSettings.

Those brackets mean that you are commenting something out, which means Corona won’t read what is between them. Step #1 should be to go through the Corona documentation and tutorials and try to build the sample applications they have, to really understand how to use the Lua language.

to answer your specific question, if you take a look at the post from 7:22 AM you’ll see the function there provides code to increase the testvalue amount, and save the table after it’s increase. Take a look at that and see if you can adapt it to your needs.