SQLite Database Connected in Multiple Windows

Being relatively new to Xojo, I am still on a steep learning curve. I have noticed that if you connect to an SQLite Database in one window, then open a second window, the database is no longer connected. Is there a way of making the database connected to several windows without having to connect it for each window individually
?? I have tried to achieve this by declaring the database “public” but this doesn’t work. Any pointers on this matter would be greatly appreciated.

Try to declare it and to connect it on the App.Open instead of each Window In case you need it globally .

See Examples -> Sample Applications -> EddiesElectronics -> Desktop for reference.

Or create a Module and place some variables there.

But, a good idea is to close a db reference once you finished with it.

the database property has to be in an app property to be available in the whole app
if it is a window property, only the window sees it, the other window doesn’t.
or like Emile says, store it in a module, global property.

Since you state you are relatively new to Xojo, let me expound on the answers given. Any properties, methods, constants, etc. contained in a Module are global by default, unless you change their scope. You can add Modules from the Insert menu. The benefit of using a Module to contain global items is that you don’t have to preface those items with the parent name, like you do for items in App (or another Window). So, if you create a property called “db” in a Module, you can refer to it from anywhere by just “db” instead of “App.db”. Note that you still initialize/connect to your database either in the App.Open or the default Window.Open. Another tip: create a boolean property (in the Module again) called “dbConnected” and set it to true when you have successfully connected to the db. Then check this property in each window’s Open event before performing any operations with the db.

Another benefit to Modules is that you can build a common set of properties, constants and methods that are useful for most apps and just copy the module from one project to another.

Jay: very good explanation.

Mike:
try to use well written (meaningfull) variable names, so it will be easy for you to (today) know wht this variable if and (tomorrow) too.

I use some "extensions) to remember the kind of variables like Open_FI for the Open FolderItem, etc.

In a recent project, I also use Prefix to get some variables grouped in the Module presentation and easier to use with autocomplete:

Open_FI: FolderItem to access to a File
Open_TIS: Reference to read a Text file

Type Op + Tab and chose the one that is shown.

Just some ideas / examples.

Thanks for your comments and assistance chaps. I never cease to be agreeably surprised at how helpful people are on this Forum !!

Thanks guys - setting the db as an App property worked a treat. Mike

If you end up also doing a web app, then you do all that in Session instead of App.