SOLVED] Sqlite with LUA

Hello,

My problem is when i make the connection and try to access the table.
I’m using the sqlite manager to create database and tables.
My database is in teh same folder of main.lua

The Error:

[C]: in function 'nrows'  
in main chunk  
Runtime error: no such table: myTable  

My Code:

[code]
require “sqlite3”

myDB=sqlite3.open(‘myBD.sqlite3’)

local function onSystemEvent(event)
if(event.type == “applicationExit”) then
myDB:close();
end
end

for a in myDB:nrows(‘select * from myTable’) do
print(a.name)
end

[/code] [import]uid: 74146 topic_id: 21219 reply_id: 321219[/import]

NOTE: When providing a file path to open(), make sure to use system.pathForFile(). Providing just a plain file name such as “my.db” will not work consistently across the simulator and devices, especially on Android (see system.ResourceDirectory).

http://developer.anscamobile.com/reference/index/sqlite3 [import]uid: 84637 topic_id: 21219 reply_id: 84073[/import]

Hello Danny,

I make how you said, but the same error happens.

The Error

Runtime error: no such table: myTable  
stack traceback:  
 [C]: ?  
 [C]: in function 'nrows'  

The code

[code]

local path = system.pathForFile(“myApp.db”, system.DocumentsDirectory)
db = sqlite3.open( path )
local function onSystemEvent(event)
if(event.type == “applicationExit”) then
db:close();
end
end

local sql = “select * from myTable”

for row in db:nrows(sql) do
print(row.nome)
end

Runtime:addEventListener(“system”, onSystemEvent)
[/code] [import]uid: 74146 topic_id: 21219 reply_id: 84197[/import]

Your first line is wrong (i presume the sql db is in the root directory)

[code]
local path = system.pathForFile(“myApp.db”, system.ResourceDirectory)
db = sqlite3.open( path )

local function onSystemEvent(event)
if(event.type == “applicationExit”) then
db:close();
end
end

local sql = “select * from myTable”

for row in db:nrows(sql) do
print(row.nome)
end

Runtime:addEventListener(“system”, onSystemEvent) [import]uid: 84637 topic_id: 21219 reply_id: 84240[/import]

when you use system.DocumentsDirectory you have to put your database in the same path as your corona sdk installation.
I made it and now works correctly.

Thanks. [import]uid: 74146 topic_id: 21219 reply_id: 84241[/import]

Sorry to ask, but what do you mean with :

‘when you use system.DocumentsDirectory you have to put your database in the same path as your corona sdk installation.’

[import]uid: 9328 topic_id: 21219 reply_id: 87101[/import]