Creating a Database and Checking its Existence

How do you create a database within the Document Directory? Also, how do you check if it was made already?

I am trying to install/copy my database to the device’s document directory so that when the game is updated/patched I don’t erase all of their hard earned game data. Thus, I need to first install it into the proper directory and make sure I don’t overwrite it by checking whether or not it exists. [import]uid: 54716 topic_id: 17917 reply_id: 317917[/import]

Here you go:
[lua]require (“sqlite3”)

–create database if it is not there
local path = system.pathForFile(“myDataBaseFileName.db”, system.DocumentsDirectory)

– open database file in memory
db = sqlite3.open( path )

–create tables (only if they do not exist)
local tablesetup = [[CREATE TABLE IF NOT EXISTS Play (ID INTEGER PRIMARY KEY AUTOINCREMENT, columnName1 TEXT, columnName2 TEXT);]]

– execute statement
db:exec( tablesetup )[/lua]

That should take care of what you are trying to do. [import]uid: 66859 topic_id: 17917 reply_id: 68459[/import]

This is what I use to check it any file exists.

[code]
myDocumentFileAndPath = system.pathForFile(“FileName.db”, system.DocumentsDirectory) --Write Access folder
myResourceFileAndPath = system.pathForFile(“FileName.db”, system.ResourceDirectory) --Protected Folder - Read only access


– CHECK TO SEE IF THE DB EXISTS, IF NOT THEN CREATE IT –

if doesDBFileExists(myDocumentFileAndPath) == false then
–open / create a new empty db file
end

if doesDBFileExists(myResourceFileAndPath) == false then
–open / create a new empty db file
end


– Check if DB File exists
– Returns true if it exists

function doesDBFileExists(sFileAndPath)

local bResults = false

if _G.debug then
print(" doesDBFileExists ->> the file path ->> " … sFileAndPath )
end

– io.open opens a file at filePath. returns nil if no file found
local file = io.open( sFileAndPath, “r” )

if file then – YES FILE OPENed -->
io.close( file )
bResults = true
else – FILE - NOT - OPENed -->
bResults = false
end

if _G.debug then
if bResults then
print (“doesDBFileExists ->> true”)
else
print (“doesDBFileExists ->> false”)
end
end

return bResults
end
[/code] [import]uid: 11860 topic_id: 17917 reply_id: 68498[/import]

hhhmmm,

A little too much there

[lua]local path = system.pathForFile( “data.txt”, system.DocumentsDirectory )
local fhd = io.open( path )

– Determine if file exists
if fhd then
print( “File exists” )
fhd.close()
else
print( “File does not exist!” )
end[/lua]

system.pathForFile returns a string…

[import]uid: 66859 topic_id: 17917 reply_id: 69571[/import]

The previous example helped me but I had to change

fhd.close()  

to

fhd:close() [import]uid: 131851 topic_id: 17917 reply_id: 92923[/import]