database issue on windows os.

Hi…I’m working on 2 os (osX and windows 7) at the same project.

I added at my project a db made by sqlite3 and on OsX the project runs. But when i work on windows the project show me an error during creation of database.

Error : Attempt to index local fileSource (a nil value)

Why that? What change between 2 os?

I allegate the code:

 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 DB = system.pathForFile(dbName, system.DocumentsDirectory) local dbNew = sqlite3.open( DB ) return dbNew end db = setUpDatabase("data.db")
local fileSource = io.open( pathSource, "r" )         local contentsSource = fileSource:read( "\*a" )

You should test the value of fileSource after the io.open call. If it fails, it will return nil then the next line will fail. It would be helpful if you had copy/pasted the entire error message and complete back trace. Can you verify the line number is the second line above?

Also you can print out the value of pathSource to make sure it’s what you think it is.

Rob

Change this line:

local fileSource = io.open( pathSource, "r" )

To:

local fileSource = io.open( pathSource, "rb" )

thanks for your help and sorry if i didn’t  answer first.

I solved it 

There was an external module that generate conflict with this function (another savage). I don’t understand why on mac it didn’t show me.

And after solved that there was another error with database corrupt. And i solve modifying io.open( pathSource, “rb” ) and io.open( pathDest, “wb” ) .

Like suggest danny.

However there is a general compendium with all differences between os and windows?

This tutorial might help you.  https://coronalabs.com/blog/2015/05/19/tutorial-initializing-a-writable-sqlite-database-from-a-read-only-database/

As for differences? Windows and OS X are radically different operating systems. For instance, on OS X, when reading in a text file, lines are traditionally separated by newline characters (CTRL-L or \n or Line Feed). Windows uses a line ending that is two characters long: carriage return + line feed (CTRL-M CTRL-L or \r\n). Windows is likely to corrupt files more than OS X will.

Rob

local fileSource = io.open( pathSource, "r" )         local contentsSource = fileSource:read( "\*a" )

You should test the value of fileSource after the io.open call. If it fails, it will return nil then the next line will fail. It would be helpful if you had copy/pasted the entire error message and complete back trace. Can you verify the line number is the second line above?

Also you can print out the value of pathSource to make sure it’s what you think it is.

Rob

Change this line:

local fileSource = io.open( pathSource, "r" )

To:

local fileSource = io.open( pathSource, "rb" )

thanks for your help and sorry if i didn’t  answer first.

I solved it 

There was an external module that generate conflict with this function (another savage). I don’t understand why on mac it didn’t show me.

And after solved that there was another error with database corrupt. And i solve modifying io.open( pathSource, “rb” ) and io.open( pathDest, “wb” ) .

Like suggest danny.

However there is a general compendium with all differences between os and windows?

This tutorial might help you.  https://coronalabs.com/blog/2015/05/19/tutorial-initializing-a-writable-sqlite-database-from-a-read-only-database/

As for differences? Windows and OS X are radically different operating systems. For instance, on OS X, when reading in a text file, lines are traditionally separated by newline characters (CTRL-L or \n or Line Feed). Windows uses a line ending that is two characters long: carriage return + line feed (CTRL-M CTRL-L or \r\n). Windows is likely to corrupt files more than OS X will.

Rob