I can query data in an SQLlite database using the corona SDK simulator but can not insert or delete data. I am running the latest public release of the Corona SDK. Here is the code. It compiles fine but the insert command simply will not work. The database name is mydatabase.sqlite and the table I am querying, trying to insert, update and delete is called projects with fields: id, name, category and rating. It auto indexes so you don’t need to insert the primary key. Here is the code for the main.lua that I am currently working on:
local sqlite3 = require “sqlite3”
local function setUpDatabase(dbName)
local path = system.pathForFile( dbName, system.DocumentsDirectory )
local file = io.open( path, “r” )
if ( file == nil ) then
– copy the database file if doesn’t exist
local pathSource = system.pathForFile( dbName, system.ResourceDirectory )
local fileSource = io.open( pathSource, “r” )
local contentsSource = fileSource:read( “*a” )
local pathDest = system.pathForFile( dbName, system.DocumentsDirectory )
local fileDest = io.open( pathDest, “w” )
fileDest:write( contentsSource )
io.close( fileSource )
io.close( fileDest )
end
local gameDB = system.pathForFile(dbName, system.DocumentsDirectory)
local dbNew = sqlite3.open( gameDB )
return dbNew
end
function loadData()
local sql = “select * from projects”
local projects = {}
for a in db:nrows(sql) do
projects[#projects+1] =
{
id = a.id,
name = a.name,
category = a.category,
rating = a.rating
}
end
return projects
end
function insertData(n, c, r)
local sql = “insert into projects (name, category, rating) values (’” … n … “’, '” … c … "’, " … r … “)”
db:exec(sql)
end
function deleteData(id)
local sql = "delete from projects where id = " … tostring(id)
db:exec(sql)
end
function updateData(id, col, v)
local sql = “update projects set " … col … " = '” … v … "’ where id = " … tostring(id)
db:exec(sql)
end
db = setUpDatabase(“mydatabase.sqlite”)
insertData(“John”,“Human”, 2)
deleteData(3)
updateData(4, “name”, “Ralph”)
--updateData(2, “category”, “Dog”)
--updateData(5, “rating”, 4)
local data = loadData()
for x = 1, #data do
print (data[x].id, data[x].name, data[x].category, data[x].rating)
end