require "sqlite3" db = sqlite3.open( system.pathForFile( "database.sqlite", system.ResourceDirectory ) ) db:exec( "CREATE TABLE IF NOT EXISTS db( id INTEGER PRIMARY KEY, name String, audio STRING, score INT )" ) function newSQLData() if not( getSQLData( "user" ) ) then db:exec( "INSERT INTO db( name, audio, score ) VALUES( 'user', 'on', '0' )" ) end end function setSQLData( row, value ) if ( getSQLData( "user" ) ) then db:exec( "UPDATE db SET '"..row.."' ='"..value.."', WHERE name = 'user'" ) end end function getSQLData( row ) if ( getSQLData( "user" ) ) then local value = db:exec( "SELECT '"..row.."' FROM db WHERE name = 'user'" ) return value end end
This crap doesn’t work. I don’t know whats wrong…
It should check if someones already registeres and if not insert the data…
Your syntax is a bit wrong because you are using SQL instead SQLite - there is no Int or String.
But main reason is function order! Your new and set function begins with call to get function which is unknown for them so the statements aren’t executed.
So far so good.
Now there is a problem…
If I call newSQLData() it prints both options to console and insert.
So it ignores the check if the user is already in database?
What is wrong… ?
EDIT:
If table isn’t present, it doesn’t create it at first time.
But if I restart the project, table gets created…
require "sqlite3" db = sqlite3.open( system.pathForFile( "database.sql", system.ResourceDirectory ) ) db:exec( "CREATE TABLE IF NOT EXISTS db( id INTEGER PRIMARY KEY, name String, audio STRING, score INT )" ) function setSQLData( row, value ) db:exec( "UPDATE db SET '"..row.."' ='"..value.."', WHERE name = 'user'" ) end function getSQLData( row ) local value = db:exec( "SELECT '"..row.."' FROM db WHERE name = 'user'" ) if ( value ) then return value else return false end end function newSQLData() if ( getSQLData( "name" ) ~= "user" ) then db:exec( "INSERT INTO db( name, audio, score ) VALUES( 'user', 'on', '0' )" ) print( "newSQLData() called INSERT" ) else return false end print( "newSQLData() called FALSE" ) end
-- database functions -- require "sqlite3" db = sqlite3.open( system.pathForFile( "database.sql", system.ResourceDirectory ) ) db:exec( "CREATE TABLE IF NOT EXISTS db( id INTEGER PRIMARY KEY, name String, audio STRING, score INT )" ) function setSQLData( data, value ) db:exec( "UPDATE db SET '"..row.."' ='"..value.."', WHERE name = 'user'" ) end function getSQLData( data ) for row in db:nrows( "SELECT \* FROM db WHERE name = 'user'" ) do if ( row.data ) then return row.data else return false end end end function newSQLData() if ( getSQLData( "name" ) ~= "user" ) then db:exec( "INSERT INTO db( name, audio, score ) VALUES( 'user', 'on', '0' )" ) print( "newSQLData() called INSERT" ) else return false end print( "newSQLData() called FALSE" ) end -- end --
Put in some print statements. Try to see what’s going on. Study the differences in what you’re doing and what the sample app is doing and try to find differences. Unfortunately, I’ve not used SQLite enough to know if what you’re doing is right or not. But its hard to tell what’s going on because you’ve not posted enough code. Based on what is there, you’re opening and creating the database then defining some functions that never get called.
I’d recommend putting in some prints and see what’s going on. Print the values you are passing in. Print out the whole query string. If you’re on a Mac, you can run sqlite from the command line and try the queries to make sure sqlite isn’t choking on something.
As Rob mentioned, you need to post more code so we can see how you’re calling your functions - or you’ll need to look at debugging the code more.
For example, what is actually being returned in your “row.data” object in getSQLData()? From the SQL structure, it looks like you’d actually be returning a lua table with three columns: user, audio, and score. Instead of returning row.data, why don’t you just return true and check for that instead?
Your syntax is a bit wrong because you are using SQL instead SQLite - there is no Int or String.
But main reason is function order! Your new and set function begins with call to get function which is unknown for them so the statements aren’t executed.
So far so good.
Now there is a problem…
If I call newSQLData() it prints both options to console and insert.
So it ignores the check if the user is already in database?
What is wrong… ?
EDIT:
If table isn’t present, it doesn’t create it at first time.
But if I restart the project, table gets created…
require "sqlite3" db = sqlite3.open( system.pathForFile( "database.sql", system.ResourceDirectory ) ) db:exec( "CREATE TABLE IF NOT EXISTS db( id INTEGER PRIMARY KEY, name String, audio STRING, score INT )" ) function setSQLData( row, value ) db:exec( "UPDATE db SET '"..row.."' ='"..value.."', WHERE name = 'user'" ) end function getSQLData( row ) local value = db:exec( "SELECT '"..row.."' FROM db WHERE name = 'user'" ) if ( value ) then return value else return false end end function newSQLData() if ( getSQLData( "name" ) ~= "user" ) then db:exec( "INSERT INTO db( name, audio, score ) VALUES( 'user', 'on', '0' )" ) print( "newSQLData() called INSERT" ) else return false end print( "newSQLData() called FALSE" ) end
-- database functions -- require "sqlite3" db = sqlite3.open( system.pathForFile( "database.sql", system.ResourceDirectory ) ) db:exec( "CREATE TABLE IF NOT EXISTS db( id INTEGER PRIMARY KEY, name String, audio STRING, score INT )" ) function setSQLData( data, value ) db:exec( "UPDATE db SET '"..row.."' ='"..value.."', WHERE name = 'user'" ) end function getSQLData( data ) for row in db:nrows( "SELECT \* FROM db WHERE name = 'user'" ) do if ( row.data ) then return row.data else return false end end end function newSQLData() if ( getSQLData( "name" ) ~= "user" ) then db:exec( "INSERT INTO db( name, audio, score ) VALUES( 'user', 'on', '0' )" ) print( "newSQLData() called INSERT" ) else return false end print( "newSQLData() called FALSE" ) end -- end --
Put in some print statements. Try to see what’s going on. Study the differences in what you’re doing and what the sample app is doing and try to find differences. Unfortunately, I’ve not used SQLite enough to know if what you’re doing is right or not. But its hard to tell what’s going on because you’ve not posted enough code. Based on what is there, you’re opening and creating the database then defining some functions that never get called.