The 1.8 release of Titanium contains several iOS 5 compatibility updates. One of the more interesting updates is the installation directory for database have changed.
In prior versions you could access the database like below:
function fetchDbFile(dbName){ Ti.API.info('We build the directory path to find ' + dbName + '.sql'); return Ti.Filesystem.getFile(Ti.Filesystem.applicationSupportDirectory, 'database', dbName + '.sql'); }; var myDbName = 'foo123'; Ti.API.info('We create a db to test our method') var testDb = Ti.Database.open(myDbName); Ti.API.info('No we get the file') var dbFile = fetchDbFile(myDbName); Ti.API.info('Did we find it? ' + dbFile.exists()); Ti.API.info('Here is the nativePath ' + dbFile.nativePath); Ti.API.info('Example Finished');
With iOS 5, Apple has introduced new guidelines that have altered the database installation directory. Databases are now installed into the Private Documents Directory. There currently is not a property for accessing this directory in the Ti.Filesystem module.
But since the Ti.Filesystem module only proxies the url request to iOS you can reference it directly. Below is a method that demonstrations how to do this.
function privateDocumentsDirectory(){ Ti.API.info('We need to open a file object to get our directory info'); var testFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory); Ti.API.info('Now we remove the Documents folder reference'); var privateDir = testFile.nativePath.replace('Documents/',''); Ti.API.info('This is our base App Directory =' + privateDir); Ti.API.info('Now we add the Private Documents Directory'); privateDir+='Library/Private%20Documents/'; Ti.API.info('Our new path is ' + privateDir); return privateDir; }; var myDbName = 'foo123'; Ti.API.info('We create a db to test our method') var testDb = Ti.Database.open(myDbName); var testNativePath = testDb.getFile().nativePath; Ti.API.info('Our nativePath to test against is ' + testNativePath + ' this is what we need to end up with'); var privateDocFolder = privateDocumentsDirectory(); Ti.API.info('Our Private Document Folde is ' + privateDocFolder); Ti.API.info("Let's see if we can find our database"); var dbCheck = Ti.Filesystem.getFile(privateDocFolder, myDbName+ '.sql'); Ti.API.info('Did we find it? ' + dbCheck.exists()); Ti.API.info('Do our file paths match? ' + (dbCheck.nativePath==testNativePath)); Ti.API.info('Example Finished');
To help work with your existing databases Appcelerator has added a getFile property onto the database object. This is extremely helpful if you are performing any back-up operations yourself. See this link to read more.