Sqlite3 Doesn't Work On Device.

Hey Corona Buddies.

I have a weird problem.

My Sqlite3 database work in Corona Simulator(1076) and in Xcode(4.6.1)

But when i test it on device, iPhone 5 and iPad the database doesn’t grab the level and score.

[lua]-- Import director class

local localGroup = display.newGroup()

local sqlite3 = require(“sqlite3”)

director = require(“director”)

localGroup:insert(director.directorView)

–Create a constantly looping background sound…

bgSound = audio.loadStream(“sounds/bgSound.mp3”) –  FIXED BY JEMMY IRAWAN , I THINK YOU’D BETTER MAKE IT GLOBAL VARIABLE, NOT LOCAL, BECAUSE IT WILL BE CALLED IN ANY SCENES.

audio.reserveChannels(1)   --Reserve its channel

audio.play(bgSound, {channel=1, loops=-1}) --Start looping the sound.

–Activate multi-touch so we can press multiple buttons at once.

system.activate(“multitouch”)

–Create some level globals used in the game itself/gameWon/gameOver scenes.

_G.amountofLevels = 15

_G.currentLevel = 1

_G.levelScore = 0  

–Create a database table for holding the high scores per level in.

–We only need a small database as we dont need to save much information.

local dbPath = system.pathForFile(“levelScores.db3”, system.DocumentsDirectory)

local db = sqlite3.open( dbPath )    

–Current 2 levels. Add more rows to make more levels available. Also remember if

–you add an extra row into this database you need to add an exta level to

–“amountofLevels” above.

local tablesetup = [[ 

        CREATE TABLE scores (id INTEGER PRIMARY KEY, highscore); 

        INSERT INTO scores VALUES (NULL, ‘0’); 

        INSERT INTO scores VALUES (NULL, ‘0’); 

        INSERT INTO scores VALUES (NULL, ‘0’);

        INSERT INTO scores VALUES (NULL, ‘0’); 

        INSERT INTO scores VALUES (NULL, ‘0’);

        INSERT INTO scores VALUES (NULL, ‘0’);

        INSERT INTO scores VALUES (NULL, ‘0’); 

        INSERT INTO scores VALUES (NULL, ‘0’); 

        INSERT INTO scores VALUES (NULL, ‘0’);

        INSERT INTO scores VALUES (NULL, ‘0’); 

        INSERT INTO scores VALUES (NULL, ‘0’);

        INSERT INTO scores VALUES (NULL, ‘0’);

        INSERT INTO scores VALUES (NULL, ‘0’); 

        INSERT INTO scores VALUES (NULL, ‘0’);

        INSERT INTO scores VALUES (NULL, ‘0’);

    ]]

db:exec( tablesetup ) --Create it now.s

db:close() --Then close the database

–Now change to the menu.

director:changeScene( “menu” )[/lua]

Any ideas?

Thanks.

Jimmi

Heres the level screen

[lua]

local M = {}

local function new()  

    --Setup the display groups we want

    local mainGroup = display.newGroup()

    --Set up our basic maths variables

    local _W = display.contentWidth --Width and height parameters

    local _H = display.contentHeight 

    --Set up some of the sounds we want to use…

    --You have to properly remove these… check the clean funciton.

    local tapChannel --Used later to get an audio channel

    local tapSound = audio.loadSound(“sounds/tapsound.wav”)

    --Vars…

    local amountOfLevels = 15 --Max number of sqaures to make

    local amountPerRow = 5 --Contorls placement.

    local levelScores = {} --Holds are level information

    --------

    – *** Create the background and Play Button ***

    -------

    --Background images first…

    local bg1 = display.newImageRect( “images/levelSelect.jpg”, 480,320)

    bg1.x = _W*0.5; bg1.y = _H*0.5

    mainGroup:insert(bg1)

    

    --------------------------------

    – *** Load all the level scores/amount of levels… ***

    --------------------------------

    --Each level score is placed in the levelScores array. 

    local dbPath = system.pathForFile(“levelScores.db3”, system.DocumentsDirectory)

    local db = sqlite3.open( dbPath ); 

    --Loop through each row and assign the score to our levelScores array.

    local rowInt = 1

    for row in db:nrows(“SELECT * FROM scores”) do   

        levelScores[rowInt] = row.highscore

        rowInt = rowInt + 1

    end

    db:close()

    ------------------------------------------------

    – *** Create all of the level buttons. Only activate the ones in the levelScores array. ***

    ------------------------------------------------

    --Level sqaure clicked…

    local function levelTouched(event)

        --Play the sound

        tapChannel = audio.play( tapSound )

        --Set our global level variable to the id of the block we just touched.

        currentLevel = event.target.id

        --Now change to our game. Which will use the above variable to create the level.

        director:changeScene(“game”, “moveFromRight”)

    end

    local menu = display.newImage(“images/back.png”)

    menu.x = 30

    menu.y = 300

    

    local function gotoMenu() director:changeScene(“menu”, “moveFromLeft”) end

    menu:addEventListener(“tap”, gotoMenu)

    --Now look through our “amountOfLevels” var creating the blocks.

    --Question mark blocks can’t be clicked as we don’t have those levels. The ones we

    --actually have in the database are loaded as a normal block, which we can click.

    local rowControl = 0

    local yControl = 0

    local xStart, xOffset = 80, 80 --Controls the spacing/placement

    local yStart, yOffset = 100, 80 --Controls the spacing/placement

    local i 

    for i=1, amountOfLevels do

        if i <= #levelScores then

            --First the sqaure

            local sqaure = display.newImageRect(mainGroup, “images/block_green_brick.png”, 50, 50)

            sqaure.x = xStart + (rowControl*xOffset)

            sqaure.y = yStart + (yControl*yOffset); sqaure.id = i

            sqaure:addEventListener(“tap”, levelTouched)

            --Then the number/score text.

            local number = display.newText(mainGroup, i,0,0,“press start”,15)

            number:setTextColor(0, 0, 0)

            number.x = sqaure.x; number.y = sqaure.y-8

            local score = display.newText(mainGroup, levelScores[i],0,0,“press start”,10)

            score:setTextColor(0, 0, 0)

            score.x = sqaure.x; score.y = number.y+20

        else

            local sqaure = display.newImageRect(mainGroup, “images/block_green_question.png”, 50, 50)

            sqaure.x = xStart + (rowControl*xOffset)

            sqaure.y = yStart + (yControl*yOffset)

        end

        --Control variables.

        rowControl = rowControl + 1

        if rowControl == amountPerRow then 

            yControl = yControl + 1

            rowControl = 0 

        end

    end

    --------------

    --Clean function…

    --------------

    local function clean()

        --Remove any sound effects.

        --They must NOT be playing to be removed.

        --audio.dispose( tapSound ); tapSound = nil; – FIXED BY JEMMY IRAWAN , AGAIN, DONT NIL IT, IT WILL BE RELEASED BY ITSELF WHEN CHANGING SCENE.

    end

    M.clean=clean

    return mainGroup

end

M.new = new

return M[/lua]

You might want to consider putting some debugging “print” statements in your SQL Query loop to see what’s getting returned.  I would also in the loop where you’re drawing the blocks print out #levelScores, etc.

Heres the level screen

[lua]

local M = {}

local function new()  

    --Setup the display groups we want

    local mainGroup = display.newGroup()

    --Set up our basic maths variables

    local _W = display.contentWidth --Width and height parameters

    local _H = display.contentHeight 

    --Set up some of the sounds we want to use…

    --You have to properly remove these… check the clean funciton.

    local tapChannel --Used later to get an audio channel

    local tapSound = audio.loadSound(“sounds/tapsound.wav”)

    --Vars…

    local amountOfLevels = 15 --Max number of sqaures to make

    local amountPerRow = 5 --Contorls placement.

    local levelScores = {} --Holds are level information

    --------

    – *** Create the background and Play Button ***

    -------

    --Background images first…

    local bg1 = display.newImageRect( “images/levelSelect.jpg”, 480,320)

    bg1.x = _W*0.5; bg1.y = _H*0.5

    mainGroup:insert(bg1)

    

    --------------------------------

    – *** Load all the level scores/amount of levels… ***

    --------------------------------

    --Each level score is placed in the levelScores array. 

    local dbPath = system.pathForFile(“levelScores.db3”, system.DocumentsDirectory)

    local db = sqlite3.open( dbPath ); 

    --Loop through each row and assign the score to our levelScores array.

    local rowInt = 1

    for row in db:nrows(“SELECT * FROM scores”) do   

        levelScores[rowInt] = row.highscore

        rowInt = rowInt + 1

    end

    db:close()

    ------------------------------------------------

    – *** Create all of the level buttons. Only activate the ones in the levelScores array. ***

    ------------------------------------------------

    --Level sqaure clicked…

    local function levelTouched(event)

        --Play the sound

        tapChannel = audio.play( tapSound )

        --Set our global level variable to the id of the block we just touched.

        currentLevel = event.target.id

        --Now change to our game. Which will use the above variable to create the level.

        director:changeScene(“game”, “moveFromRight”)

    end

    local menu = display.newImage(“images/back.png”)

    menu.x = 30

    menu.y = 300

    

    local function gotoMenu() director:changeScene(“menu”, “moveFromLeft”) end

    menu:addEventListener(“tap”, gotoMenu)

    --Now look through our “amountOfLevels” var creating the blocks.

    --Question mark blocks can’t be clicked as we don’t have those levels. The ones we

    --actually have in the database are loaded as a normal block, which we can click.

    local rowControl = 0

    local yControl = 0

    local xStart, xOffset = 80, 80 --Controls the spacing/placement

    local yStart, yOffset = 100, 80 --Controls the spacing/placement

    local i 

    for i=1, amountOfLevels do

        if i <= #levelScores then

            --First the sqaure

            local sqaure = display.newImageRect(mainGroup, “images/block_green_brick.png”, 50, 50)

            sqaure.x = xStart + (rowControl*xOffset)

            sqaure.y = yStart + (yControl*yOffset); sqaure.id = i

            sqaure:addEventListener(“tap”, levelTouched)

            --Then the number/score text.

            local number = display.newText(mainGroup, i,0,0,“press start”,15)

            number:setTextColor(0, 0, 0)

            number.x = sqaure.x; number.y = sqaure.y-8

            local score = display.newText(mainGroup, levelScores[i],0,0,“press start”,10)

            score:setTextColor(0, 0, 0)

            score.x = sqaure.x; score.y = number.y+20

        else

            local sqaure = display.newImageRect(mainGroup, “images/block_green_question.png”, 50, 50)

            sqaure.x = xStart + (rowControl*xOffset)

            sqaure.y = yStart + (yControl*yOffset)

        end

        --Control variables.

        rowControl = rowControl + 1

        if rowControl == amountPerRow then 

            yControl = yControl + 1

            rowControl = 0 

        end

    end

    --------------

    --Clean function…

    --------------

    local function clean()

        --Remove any sound effects.

        --They must NOT be playing to be removed.

        --audio.dispose( tapSound ); tapSound = nil; – FIXED BY JEMMY IRAWAN , AGAIN, DONT NIL IT, IT WILL BE RELEASED BY ITSELF WHEN CHANGING SCENE.

    end

    M.clean=clean

    return mainGroup

end

M.new = new

return M[/lua]

You might want to consider putting some debugging “print” statements in your SQL Query loop to see what’s getting returned.  I would also in the loop where you’re drawing the blocks print out #levelScores, etc.

In case you have not solved the problem, you have to give read / write access to your database “levelScores.db3” for everyone.

In case you have not solved the problem, you have to give read / write access to your database “levelScores.db3” for everyone.