I finally got the connection with the database up and running, doing everything I needed for the program I am working on! (I bought the PDF “Wish I Knew How To Program SQLite with Xojo API2” which was really helpful!).
At the moment when I start the program in Xojo, I am asked to choose the database file so the connection can made and data can be loaded. Is there a way to avoid this and get the connection made without being asked for it?
you could make a method File
and it returns the sqlite database file as FolderItem
with SpecialFolder. you can access the most useful paths.
for a few apps for my own use i put the database files here \Documents\Database
my DB class with Super set to SQLiteDatabase
Public Sub Constructor()
// Calling the overridden superclass constructor.
Self.DatabaseFile = File()
Public Sub Destructor()
Public Function File() As FolderItem
Var f As FolderItem = SpecialFolder.Documents.Child("Database").Child("Markus.Xojo.sqlite")
I am not understanding what you mean completely.
Right now, the database is opened with a method which is being called in the Open event from a button on WinMain. It opens a dialogue window so I can choose the database file.
I tried placing the database file in the same folder in the finder as the Xojo project file, hoping it would find it this way, but that doesn’t work.
if this database is a static ressource file use buidstep to copy it at build together with the exe.
(that means you copy a db template over into the build folder.)
u have to use FolderItem correct that you can open a file.
use a full path to your file.
Until you explicitely tell your application what to do (or teh computer all of a sudden became intelligent), this will never happens.
The project file is a developer concept.
In the eyes of a user, all (s)he can see is an application. And this application belongs to the Applications folder (even if YOU can place it where you want).
Never, ever store a file or folder beside your application / inside the Applications folder if you want to write / change it.
That is what SpecialFolder are used to.
In your case, and if the user do not have to know where the data base file is, you can place it in the Application Support folder.
Never use .Child(“my folder”).Child(“my nested folder”): use one .Child in a line and test if it is not nil and exists before using it.
You never go there:
because you have an answer to do what you ask…
Look at the example below EncryptionKey As String (and skip the EncryptionKey part…)
I don’t understand what you are saying here, but I will study the tips more deeply. Thanks.
Yopu have two kinds of examples that shows you how to select/open a data base file.
Var dbFile As FolderItem = FolderItem.ShowOpenFileDialog("")
and nearly all other examples in the page is:
Var dbFile As New FolderItem("MyDB.sqlite")
and you wanted to know the second (and most popular) example.
Yes, I am using this code to open the DB, I learned this in the PDF I mentioned above. What I would like to do now is to get this done without having to choose the DB manually. But maybe that isn’t really so important. Maybe I am asking for something that isn’t normally done.
On application start will the application expect that a database should exist, or will the application build a new empty database if it doesn’t find one in the expected location with the expected name?
You do not need to present an open dialog to open the database at application start if the application database is expected to be in a specific location. You can silently open it from that location at start and if not found inform the user to find a database or create a new one if not found.
A suggested location for the database that the application would look for the database file but you would replace database name with the name you are using:
Before more advice can be given you really need to make and state your design decisions on whether the application will have a database provided with it, or will you include the the ability to build an empty database at any time. If you include building one you may also want to have a preferences function to open alternate databases. It really depends on you stating clearly exactly what you want your application to do in regards to handling the database file.
It’s simple enough.
Var f as folderitem, db as SQLiteDatabase f = new folderitem ("/path/to/your/database/mydatabase.db", FolderItem.PathModes.Native)
db = new SQLiteDatabase
db.file = f
Add your own error handling to taste.
The program comes with a database included that already has data inside. And it’s indeed this what I am trying to do: open the database silently.
@TimStreater gave you the short answer without any error checking or checking to see if the path exists. Those are things you should add to make the opening of the db more bulletproof.
You also have to decide where to put thedatabase and then to adjust the path in my example appropriately.