# Basic question on SQLiteDatabases

**URL:** <https://forum.xojo.com/t/basic-question-on-sqlitedatabases/61425>\
**Category:** iOS\
**Created:** [February 24, 2021, 7:12am UTC](https://forum.xojo.com/t/basic-question-on-sqlitedatabases/61425 "2021-02-24T07:12:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christoph\_Emrich](https://forum.xojo.com/user_avatar/forum.xojo.com/christoph_emrich/32/1727_2.png) [@Christoph\_Emrich](https://forum.xojo.com/u/Christoph_Emrich)\
**Post date:** [February 24, 2021, 7:12am UTC](https://forum.xojo.com/t/basic-question-on-sqlitedatabases/61425/1 "2021-02-24T07:12:40Z")

</div>

When I connect to a file-based SQLiteDatabase `db`, what’s common practice:  
a) Check with `db.connect` every time before I run an SQL statement  
b) Do `db.connect` once when I start the app and it will never disconnect.

I mean, the real question is, will a file-based SQLiteDatabase ever disconnect, for example, when the user exits the app and comes back later?

---

<div class="post-metadata">

**Author:** ![Wayne\_Golding](https://forum.xojo.com/user_avatar/forum.xojo.com/wayne_golding/32/165_2.png) [@Wayne\_Golding](https://forum.xojo.com/u/Wayne_Golding)\
**Post date:** [February 24, 2021, 7:27am UTC](https://forum.xojo.com/t/basic-question-on-sqlitedatabases/61425/2 "2021-02-24T07:27:41Z")

</div>

For a SQLite database b is the answer and this is consistent on all platforms but not for all database engines.

---

<div class="post-metadata">

**Author:** ![David\_Cox](https://forum.xojo.com/user_avatar/forum.xojo.com/david_cox/32/227_2.png) [@David\_Cox](https://forum.xojo.com/u/David_Cox)\
**Post date:** [February 24, 2021, 8:20am UTC](https://forum.xojo.com/t/basic-question-on-sqlitedatabases/61425/3 "2021-02-24T08:20:30Z")

</div>

When the user exit the app the connection will be closed automatically.

For my Preferences database file I connect in the Open event and leave it open, never closing it until the app closes.

---

<div class="post-metadata">

**Author:** ![Christoph\_Emrich](https://forum.xojo.com/user_avatar/forum.xojo.com/christoph_emrich/32/1727_2.png) [@Christoph\_Emrich](https://forum.xojo.com/u/Christoph_Emrich)\
**Post date:** [February 24, 2021, 9:02am UTC](https://forum.xojo.com/t/basic-question-on-sqlitedatabases/61425/4 "2021-02-24T09:02:07Z")

</div>

So, if I understand properly, it’s like this: only if the app is being closed completely, i.e., it is not open any longer in the background (task manager), only then will the database disconnect.

As long as the user exits the app but the app is still accessible through the task manager, the database connection is also still there. Which means, as you both say, I will only connect to the database file in the App.Open event handler.

Thank you!
