Inserting data into the sqlite database with the Corona SDK simulator
source link: https://www.codesd.com/item/inserting-data-into-the-sqlite-database-with-the-corona-sdk-simulator.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Inserting data into the sqlite database with the Corona SDK simulator
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. Here is the code for the main.lua:
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("Horse Crazy", "Game", 2)
deleteData(3)
--updateData(4, "name", "Ralph")
--updateData(4, "category", "Dog")
--updateData(4, "rating", 4)
local data = loadData()
for x = 1, #data do
print (data[x].id, data[x].name, data[x].category, data[x].rating)
end
Just figured out after having same problem for a while. You have to replace this line
local sql = [[delete from projects WHERE ID = ']]..id..[[';]]
db:exec(sql)
make sure id is a number otherwise simply make it a number by adding this before first line:
id=id*1
Recommend
-
5
The Importance of Inserting NO-OPs Into Your Programming Day Sep 12, 2016 One of the better development techniques that I've ever found has nothing to do with: programming language edit...
-
15
ServerSide ActionScript Tutorial : Inserting Data into a Database Saturday, June 8, 2002 Martin Wood has put together a tutorial on ServerSide ActionScript showing how to insert data passed from Flash via Flash Remoting...
-
10
SQL SERVER – Inserting sp_who2 Into a TableEarlier this week I blogged about SQL SERVER – Representing sp_who2 with DMVs
-
9
Inserting Form Values into an Excel Worksheet Using VBA advertisements I'm trying to insert form values into my Excel spreadsheet using vba...
-
9
Inserting One Billion Rows in SQLite Under A Minute17 Jul 2021Current Best: 100M rows inserts in 33 seconds. (you can check the source code on Github)
-
9
Inserting 130M SQLite Rows per Minute...from a Scripting Language The other week, I stumbled on the post Inserting One Billion Rows in SQLite Under A Minute, which was a...
-
6
Inserting data into the SQL table from the HTML form advertisements A HTML form has been created that should (when filled) send the data it's...
-
13
PHP & amp; Inserting a blank MYSQL database advertisements OK so I am new to php,ajax and mysql and I have searched the foru...
-
8
Excel gets new function for inserting images into cells...
-
8
Introduction This article shows you how to insert the profile information of any user into the react navigation bar, so as to make it sign-in aware. It shows you how to insert and configure the profile picture & menu, how to reuse the sig...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK