[Resolved] [IPHONE] - Problem with building Sqlite

I have a problem with sqlite database. I have made a script based on this script.

[lua]–Include sqlite
require “sqlite3”
–Open data.db. If the file doesn’t exist it will be created
local path = system.pathForFile(“data.db”, system.DocumentsDirectory)
db = sqlite3.open( path )

–Handle the applicationExit event to close the db
local function onSystemEvent( event )
if( event.type == “applicationExit” ) then
db:close()
end
end

–Setup the table if it doesn’t exist
local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content, content2);]]
print(tablesetup)
db:exec( tablesetup )

–Add rows with a auto index in ‘id’. You don’t need to specify a set of values because we’re populating all of them
local testvalue = {}
testvalue[1] = ‘Hello’
testvalue[2] = ‘World’
testvalue[3] = ‘Lua’
local tablefill =[[INSERT INTO test VALUES (NULL, ‘]]…testvalue[1]…[[’,’]]…testvalue[2]…[[’);]]
local tablefill2 =[[INSERT INTO test VALUES (NULL, ‘]]…testvalue[2]…[[’,’]]…testvalue[1]…[[’);]]
local tablefill3 =[[INSERT INTO test VALUES (NULL, ‘]]…testvalue[1]…[[’,’]]…testvalue[3]…[[’);]]
db:exec( tablefill )
db:exec( tablefill2 )
db:exec( tablefill3 )

–print the sqlite version to the terminal
print( "version " … sqlite3.version() )

–print all the table contents
for row in db:nrows(“SELECT * FROM test”) do
local text = row.content…" "…row.content2
local t = display.newText(text, 20, 30 * row.id, null, 16)
t:setTextColor(255,0,255)
end

–setup the system listener to catch applicationExit
Runtime:addEventListener( “system”, onSystemEvent )[/lua]
My script looks a bit different but the same problem is at both examples. The example from above works fin on corona simulator but not on iPhone the same problem is at mine. My script works very fine in corona simulator but not on iPhone too.

So my question ist could anybody tell me what the problem is? Is it a build failure? Or does the iPhone have to get any permissions? And why doesn´t it work on iPhone but in corona simulator? And in the iOS Sdk it does´t work too if i built it for Xcode. I don´t know what to do please help.

I could send you my script if you want but it is similar to the Skript above with some difference because i made a favorite list where the sqlite database ist used with buttons.

Lenny [import]uid: 149464 topic_id: 28840 reply_id: 328840[/import]

You seem to have spammed this in several sections, which makes the forum really messy :frowning:
http://developer.coronalabs.com/forum/2011/05/05/forum-rules-and-guidelines

You mention your code doesn’t work - does the code above work for you on device? [import]uid: 52491 topic_id: 28840 reply_id: 116232[/import]

Sorry it is very urgent so i would make the chance higher that someone replies to this. I delete the others i´m sorry.

And no the code above does´t work on device this code and my code work on corona simulator but not on device. [import]uid: 149464 topic_id: 28840 reply_id: 116237[/import]

Have you tried the SQLite demo in CoronaSDK > SampleCode > Storage > SQLite ?

Is that working on your device? [import]uid: 52491 topic_id: 28840 reply_id: 116303[/import]

No that does´t work too. Although my script is the same as this one instead of some modification and inserting data with buttons. [import]uid: 149464 topic_id: 28840 reply_id: 116355[/import]

If that isn’t working then it sounds like a bug - are you able to file using the link in the top right, please? (In steps to reproduce simply mention the sample code that comes with CoronaSDK download.) [import]uid: 52491 topic_id: 28840 reply_id: 116417[/import]

I don´t know why but now both of the scripts work on device the one at the front of this page and the one in the sample code but mine doesn´t work [import]uid: 149464 topic_id: 28840 reply_id: 116429[/import]

[lua]require “sqlite3”

local path_db = system.pathForFile(“data.db”, system.DocumentsDirectory)
db = sqlite3.open( path_db )

–Handle the applicationExit event to close the db
local function onSystemEvent( event )
if( event.type == “applicationExit” ) then
if db:isopen() then
db:close()
end
end
end

–Setup the table if it doesn’t exist
local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content, content2);]]
db:exec( tablesetup )

local function favoriten_plus (event)
–Add rows with a auto index in ‘id’. You don’t need to specify a set of values because we’re populating all of them
local testvalue = {}
testvalue[1] = data_eintrag
testvalue[2] = ‘Gastro’
local tablefill =[[INSERT INTO test VALUES (NULL, ‘]]…testvalue[1]…[[’,’]]…testvalue[2]…[[’);]]
db:exec( tablefill )
end
local function fav_touch ( event )
if event.phase==“ended” then

transition.to(detail_fav_aktiv, {time=1, x=0, transition=easing.outExpo })

timer.performWithDelay(50, favoriten_plus)

end
return true
end
local function fav_aktiv_touch ( event )
if event.phase ==“ended” then
local delete_sql = [[DELETE FROM test WHERE content=’]]… data_eintrag …[[’;]]
db:exec( delete_sql )

local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content);]]
db:exec( tablesetup )

transition.to(detail_fav_aktiv, {delay=100,time=1, x=display.contentWidth+600, transition=easing.outExpo })
end
return true
end[/lua]
that is a part of my script where i generate and delete the data of my sqlite database can you find some problems inside and where i switch the favorite button to aktiv by transition.to of two buttons

data_eintrag is an id of an entry which is later used to make a php request [import]uid: 149464 topic_id: 28840 reply_id: 116428[/import]

There must be a bug in your code somewhere…try to clean up all sql statements and use regular strings instead.

  
local sqlStr = "Update myTable set phone = 123, name='Some Joe' where id=1";  
db:exec( sqlStr );  
  

Much easier to debug, understand and do a printout of the statements. Use a GUI to the database and run the queries to see that you build them up correctly. I am using Base for my sqlite database. [import]uid: 81188 topic_id: 28840 reply_id: 116433[/import]

you mean something like this:

[lua]local tablefill =“INSERT INTO test VALUES id=NULL, content=123, content2=gastro”
db:exec( tablefill )[/lua]

instead of

[lua] local tablefill =[[INSERT INTO test VALUES (NULL, ‘]]…testvalue[1]…[[’,’]]…testvalue[2]…[[’);]]
db:exec( tablefill )[/lua] [import]uid: 149464 topic_id: 28840 reply_id: 116434[/import]

Yes, exactly what I mean. Easier to see the complete query and easy to printout and debug with some type of enterprise manager for sqlite.

Joakim [import]uid: 81188 topic_id: 28840 reply_id: 116435[/import]

ok i will try hope that this gonna work because i didn´t understand why my script works well in corona simulator but not on device or Xcode. Thank you so far. I would write when i´m done to change the arguments and if it works or not.

it is the first time that i work with an sqlite database so i tried to do it like the example and modified the example. [import]uid: 149464 topic_id: 28840 reply_id: 116436[/import]

I just tried the first script you posted on this forum thread, and it works fine both in simulator and on an iPad2. So if thats all the code you have tested with, it’s fine. Or are you hiding some other code? :wink:

Joakim [import]uid: 81188 topic_id: 28840 reply_id: 116439[/import]

[lua]local function favoriten_plus (event)
–Add rows with a auto index in ‘id’. You don’t need to specify a set of values because we’re populating all of them
local testvalue = {}
testvalue[1] = data_eintrag
testvalue[2] = ‘Gastro’
local tablefill =“INSERT INTO test VALUES id=NULL, content=”…data_eintrag…", content2=gastro"
db:exec( tablefill )
end [/lua]

doesn´t work db[1] = nil row.content=nil and row.content2 = nil and the database does´t work any more in corona simulator [import]uid: 149464 topic_id: 28840 reply_id: 116440[/import]

the first one was an example my whole part for sqlite script is:

[lua]require “sqlite3”

local path_db = system.pathForFile(“data.db”, system.DocumentsDirectory)
db = sqlite3.open( path_db )

–Handle the applicationExit event to close the db
local function onSystemEvent( event )
if( event.type == “applicationExit” ) then
if db:isopen() then
db:close()
end
end
end

–Setup the table if it doesn’t exist
local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content, content2);]]
db:exec( tablesetup )

local function favoriten_plus (event)
–Add rows with a auto index in ‘id’. You don’t need to specify a set of values because we’re populating all of them
local testvalue = {}
testvalue[1] = data_eintrag
testvalue[2] = ‘Gastro’
local tablefill =[[INSERT INTO test VALUES (NULL, ‘]]…testvalue[1]…[[’,’]]…testvalue[2]…[[’);]]
db:exec( tablefill )

–for row in db:nrows(“SELECT * FROM test”) do
–print (row.content)
–end
end
local function fav_touch ( event )
if event.phase==“ended” then

transition.to(detail_fav_aktiv, {time=1, x=0, transition=easing.outExpo })

timer.performWithDelay(50, favoriten_plus)

–print (“INAKTIV”)

end
–local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content);]]

return true
end
local function fav_aktiv_touch ( event )
if event.phase ==“ended” then
local delete_sql = [[DELETE FROM test WHERE content=’]]… data_eintrag …[[’;]]
db:exec( delete_sql )

local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content);]]
db:exec( tablesetup )

print (“AKTIV”)
transition.to(detail_fav_aktiv, {delay=100,time=1, x=display.contentWidth+600, transition=easing.outExpo })
–transition.to(detail_fav_aktiv, {time=100, alpha=0, transition=easing.outExpo })
end
return true
end[/lua]

in one file and in another file to generate a tableview with the favorites which are generated before it is this script

[lua]local path_db = system.pathForFile(“data.db”, system.DocumentsDirectory)
db = sqlite3.open( path_db )

–Handle the applicationExit event to close the db
local function onSystemEvent( event )
if( event.type == “applicationExit” ) then
if db:isopen() then
db:close()
end
end
end

–Setup the table if it doesn’t exist
local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content, content2);]]
db:exec( tablesetup )

local function favoriten_plus (event)
–Add rows with a auto index in ‘id’. You don’t need to specify a set of values because we’re populating all of them
local testvalue = {}
testvalue[1] = data_eintrag
testvalue[2] = ‘Gastro’
local tablefill =[[INSERT INTO test VALUES (NULL, ‘]]…testvalue[1]…[[’,’]]…testvalue[2]…[[’);]]
db:exec( tablefill )

–for row in db:nrows(“SELECT * FROM test”) do
–print (row.content)
–end
end
local function fav_touch ( event )
if event.phase==“ended” then

transition.to(detail_fav_aktiv, {time=1, x=0, transition=easing.outExpo })

timer.performWithDelay(50, favoriten_plus)

–print (“INAKTIV”)

end
–local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content);]]

return true
end
local function fav_aktiv_touch ( event )
if event.phase ==“ended” then
local delete_sql = [[DELETE FROM test WHERE content=’]]… data_eintrag …[[’;]]
db:exec( delete_sql )

local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content);]]
db:exec( tablesetup )

print (“AKTIV”)
transition.to(detail_fav_aktiv, {delay=100,time=1, x=display.contentWidth+600, transition=easing.outExpo })
–transition.to(detail_fav_aktiv, {time=100, alpha=0, transition=easing.outExpo })
end
return true
end

local function fav_bestuecken ( event )

transition.to(detailScreen, {time=400, x=display.contentWidth, transition=easing.outExpo })
transition.to(myList, {time=400, x=0, transition=easing.outExpo })
transition.to(detail_zurueck_btn, {time=1, x=oben_links_leer.x, transition=easing.outExpo })
transition.to(detail_karte_oben_rechts, {time=1, x=oben_rechts_leer.x, transition=easing.outExpo })

delta, velocity = 0, 0

if ( section == 3 ) then

–print (“ERREICHT”)

else

local function networkListener ( event )
if ( event.isError ) then
print( “Network error!”)

else
print ( "RESPONSE: " … event.response )
jSon_result = dkjson.decode(event.response)

entrycount = jSon_result.entrycount

for k,v in pairs(jSon_result) do
–print (k,v)
local b

for b=1, 999 do
if (k== “entry_”…b) then
for row in db:nrows(“SELECT * FROM test”) do
–print ( row.content )
if ( v.name~=nil and v.street~=nil and v.zip ~= nil and v.city ~= nil and v.thumb ~= nil and row.content == v.id ) then

array[w]={}
array[w].id=v.id
array[w].name=v.name
array[w].street=v.street
array[w].zip=v.zip
array[w].city=v.city
array[w].thumb=v.thumb
array[w].number=w

local path = system.pathForFile( “list_fav”… w …".png", system.DocumentsDirectory )
myFile = io.open( path, “w+b” )

– Request remote file and save data to local file
http.request{
url = “http:…”… v.thumb,
sink = ltn12.sink.file(myFile),
}

timer.performWithDelay(500, fav_bestuecken )

data[w] = {}
data[w].id=array[w].id
data[w].title = array[w].name
data[w].subtitle = array[w].street
data[w].number = array[w].number

table.insert(data, data[i])

w = w+1
else
timer.performWithDelay(500, fav_bestuecken )
end
end

end
end
end

end
if ( section == 3 and request_phase == true ) then
–print (“GDS”)
timer.performWithDelay ( 1000 , liste_bestuecken )
request_phase = false
end
end
section = section + 1
responseJSon = network.request( “http:…function=” … mime.b64(field) … “&section=” … mime.b64(section), “GET”, networkListener )
–print (section)
end

end[/lua]

and this works well on corona simulator but not on iPhone [import]uid: 149464 topic_id: 28840 reply_id: 116441[/import]

Start by simplifying your insert statement to just 1 content and do a print to make sure it is formatting correctly.
When I tested the code, I found that you needed additional single quotes if you wanted the content to be treated as strings:
[lua]local tablefill =“INSERT INTO test VALUES id=NULL, content=’”…data_eintrag…"’, content2=‘gastro’"[/lua] [import]uid: 34131 topic_id: 28840 reply_id: 116442[/import]

i have wrote a simple main.lua to check if it work but it doesn`t

[lua]-- Add onscreen text
local label1 = display.newText( “SQLite demo”, 20, 30, native.systemFontBold, 20 )
label1:setTextColor( 190, 190, 255 )
local label2 = display.newText( “Creates or opens a local database”, 20, 50, native.systemFont, 14 )
label2:setTextColor( 190, 190, 255 )
local label3 = display.newText( “(Data is shown below)”, 20, 90, native.systemFont, 14 )
label3:setTextColor( 255, 255, 190 )

–Include sqlite
require “sqlite3”
–Open data.db. If the file doesn’t exist it will be created
local path = system.pathForFile(“mydata.db”, system.DocumentsDirectory)
db = sqlite3.open( path )

–Handle the applicationExit event to close the db
local function onSystemEvent( event )
if( event.type == “applicationExit” ) then
db:close()
end
end

–Setup the table if it doesn’t exist
local tablesetup = “CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content, content2)”
print(tablesetup)
db:exec( tablesetup )

–Add rows with a auto index in ‘id’. You don’t need to specify a set of values because we’re populating all of them
local testvalue = {}
local data_eintrag=377
local tablefill =“INSERT INTO test VALUES id=NULL, content=’”…data_eintrag…"’, content2=‘gastro’"
db:exec( tablefill )
print (tablefill)

–print the sqlite version to the terminal
print( "version " … sqlite3.version() )

–print all the table contents
for row in db:nrows(“SELECT * FROM test”) do
local text = row.content
local t = display.newText(text, 20, 120 + (20 * row.id), native.systemFont, 16)
t:setTextColor(255,0,255)
end

–setup the system listener to catch applicationExit
Runtime:addEventListener( “system”, onSystemEvent )[/lua]

[import]uid: 149464 topic_id: 28840 reply_id: 116445[/import]

Ok my Skript is ok the problem is that he doesn´t generate a db file on device. I have used another script where he has no problems to generate this file and named it as my database file. Now everything works well.

The question is how can i delete this file if the app is deleted? The other problem witch the file generating can i solve i thing i doesn´t know why he doesn´t it with my script but with another instead of my and this script are exactly the same. [import]uid: 149464 topic_id: 28840 reply_id: 116511[/import]

How can i write in lua that if the app will be deleted he should delete the data.db file too??? [import]uid: 149464 topic_id: 28840 reply_id: 116512[/import]

I think that all related document and folders are automatically deleted by iOS.

Joakim [import]uid: 81188 topic_id: 28840 reply_id: 116548[/import]