Corona Lua SQLite

this is my first question on stackOverflow. I'm working with Corona and I'm having an issue accessing a SQLdb (I'm a bit of a SQL noob.)

I'm trying to access and return a value I've stored in the database. Here's some code samples:

print("---------------- How I Create New Player Save Data")
local entry = [[CREATE TABLE IF NOT EXISTS playerData (key STRING PRIMARY KEY, content INTEGER);]]
db:exec(entry)

entry = [[INSERT INTO playerData VALUES ("LastLoginTime", 0);]]
db:exec( entry )
entry = [[INSERT INTO playerData VALUES ("Credits", 1000);]]
db:exec( entry )
entry = [[INSERT INTO playerData VALUES ("Level", 1);]]
db:exec( entry )

Now this function works, it will print everything in the db (i pass in 'dbName'):

--print all the table contents
for row in db:nrows("SELECT * FROM "..dbName) do
  local text = row.key..":  "..row.content
end

This doesn't work, it returns '0':

local grabCredits = "SELECT content FROM playerData WHERE key='Credits'"
  local credits = db:exec(grabCredits)
  print("-- value: "..credits)

Neither does this, also returns '0':

local grabCredits = "SELECT key FROM playerData WHERE content>=10"
  local credits = db:exec(grabCredits)
  print("-- value: "..credits)

I don't understand what I'm doing wrong. Maybe I need to use another function call on the db other than exec(). I realize I could iterate through the db every time I want to access a single entry, but that just seems inefficient.

Any help is very much appreciated.

Answers


If you want results, you must use some form of iterator. SQLite always returns rows for a query result.


This is similar to what I'm using to retrieve one result from my database and works well for me.

local query = "SELECT content FROM playerData WHERE key = 'Credits' LIMIT 1"

local queryResultTable = {}

local queryFunction = function(userData, numberOfColumns, columnValues, columnTitles)

    for i = 1, numberOfColumns do

        queryResultTable[columnTitles[i]] = columnValues[i]

    end

end

db:exec(query, queryFunction)

for k,v in pairs(queryResultTable) do

    print(k,v)

end

Need Your Help

Convert jQuery Colorbox to act as a user dialog?

jquery colorbox

Is there a nice and easy way to have a Colorbox act as a dialog window? I understand there are enough events which can be addressed but I'm unable to construct this in a nifty way... Or are there

OpenNI Samples on Linux

linux ubuntu kinect openni point-cloud-library

I just installed OpenNI so that I can run my Kinect on Ubuntu and develop there as well and it seems to be working properly but when I run the samples it just says starting to run and then never mo...