I found this code somewhere in the forums. What it does is check if the sqlite database exists in the system.DocumentsDirectory. If it does not, then it copies the database from the system.ResourceDirectory to the system.DocumentsDirectory and then opens it.
The issue is that somehow the database is being corrupted by the simulator on Windows. If I just open the database in the system.ResourceDirectory it works fine. If I use this code to copy it, my queries come back empty. The copied database is different in size by a few bytes. If I go into the project sandbox and open the copied database with Navicat, it tells me that the database disk image is malformed.
I don’t have this problem on the device or on my mac, just my Windows 7 x64 machine. I haven’t tested it on any other version of Windows.
Is something wrong with this code or is this a bug?
Here’s the code:
[code]
dbName = “sqlite.db”
– See if Database File Already Exists in Documents Directory
path = system.pathForFile( dbName, system.DocumentsDirectory )
file = io.open( path, “r” )
if( file == nil )then
– Doesn’t Already Exist, So Copy it In From Resource Directory
pathSource = system.pathForFile( dbName, system.ResourceDirectory )
fileSource = io.open( pathSource, “r” )
contentsSource = fileSource:read( “*a” )
– Write Destination File in Documents Directory
pathDest = system.pathForFile( dbName, system.DocumentsDirectory )
fileDest = io.open( pathDest, “w” )
fileDest:write( contentsSource )
– Done
io.close( fileSource )
io.close( fileDest )
end
– One Way or Another The Database File Exists Now
– So Open Database Connection
local path = system.pathForFile(dbName, system.DocumentsDirectory)
db = sqlite3.open( path )
[/code]
[import]uid: 61552 topic_id: 12230 reply_id: 312230[/import]