For some reason I always have problems with folder items. I have a SQLite db I want to store with the application. However I can’t seem to figure out haw to get the Apps Path.
So how do I open a SQLite DB in the same folder as the application?
Drag the db into your project
Lets say your app is called ‘Banana’
When the app starts, if there is no folder called Banana in specialfolder.applicationdata, create one
If there is no db inside that, copy the one you have in resources to the banana folder
[code]dim f as folderitem
dim db as folderitem
f = specialfolder.applicationdata.child(“Banana”)
if not(f.exists) then
f.createasfolder
end if
db= f.child(“Mydb.sqllite3”)
if not (db.exists) then
//copy or create the db file here
end if[/code]
If you try to keep the writable file in Applications, OSX wont like it
If you try to keep a writeable file in Program Files on Windows, Windows wont like it
If you want to access files copied into your app via a CopyFiles step you should choose the route that works best for you. If you’re using the namespaced framework the FolderItem there has GetResource. If you’re using the classic framework, use TPSF.Resources: https://github.com/devtimi/tpsf
Note that connecting to SQLite with Xojo creates temporary files, so you cannot connect to a database that is in your app’s resources on Mac, you need to copy it somewhere first. If you’re only reading, I would recommend copying to SpecialFolder.Temporary, if you need the data later copy it to TPSF.AppSupport (which does as Jeff describes on both platforms magically for you).
It may be working for now, but don’t count on it. Configuration belongs in SpecialFolder.ApplicationData. User data belongs either there or in Documents, depending on whether you want the user to be able to access the file outside of your app.
Good advice. A note to add though:
Don’t just save to Documents. If your user needs to access the data outside the app, use a SaveAsDialog. If the app doesn’t need to allow the user access the data (like a shoebox app), store it in SpecialFolder.ApplicationData.
Treat the user’s system like you’re walking into the home of someone with OCD.
Just because you don’t care if things are a mess doesn’t mean they share that attitude.
I’m trying to use TPSF as @Tim Parnell recommended and getting an exception, here is the code:
Dim dbFile As FolderItem
dbFile = SpecialFolder.GetResource("ASPEdata.sqlite")
If Not TPSF.AppSupport.Child("ASPEdata.sqlite").Exists Then ' <<<< Getting NilObject Exception
// Only copy the file if it is not already there
Dim dest As FolderItem = TPSF.AppSupport
dbFile.CopyFileTo(dest)
End If
I fave the build step copy files putting the DB in the resource folder. What am I doing wrong?
What’s the exception? The TPSF module will give you the folder items, but it is not responsible for checking the validity of if they exist (the same way SpecialFolder works). If this is the first time you’re using the AppSupport folder, check that it exists and create it if not.
The TPSF.AppSupport folder gets you a FolderItem for the child of SpecialFolder.ApplicationData that is yours, but doesn’t create it. I suppose that could be documented a little better in the readme.
In App.Open you could:
[code]if TPSF.AppSupport.Exists = false then
TPSF.AppSupport.CreateAsDirectory
end
[/code]
and that’s all you’ll need to set it up
I may have edited my post to make it more detailed while you were posting. I think I covered why the exception is happening, and the example code should get you up and running
The TPSF.AppSupport FolderItem is the child of SpecialFolder.ApplicationData that is yours. It’s a small convenience so that you don’t have to keep doing .Child(“MyApp”)