# sqlite - Operation cannot be completed because the database is closed

**URL:** <https://forum.xojo.com/t/sqlite-operation-cannot-be-completed-because-the-database-is-closed/23906>\
**Category:** Databases\
**Created:** [April 28, 2015, 3:12pm UTC](https://forum.xojo.com/t/sqlite-operation-cannot-be-completed-because-the-database-is-closed/23906 "2015-04-28T15:12:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rich\_H](https://forum.xojo.com/user_avatar/forum.xojo.com/rich_h/32/18188_2.png) [@Rich\_H](https://forum.xojo.com/u/Rich_H)\
**Post date:** [April 28, 2015, 3:12pm UTC](https://forum.xojo.com/t/sqlite-operation-cannot-be-completed-because-the-database-is-closed/23906/1 "2015-04-28T15:12:51Z")

</div>

I am in the process of attaching a sqlite database in which I am getting an error of: Operation cannot be completed because the database is closed. to help isolate it, I created a brand new project.

if I use the following code, it generates the error:

[code] app.pantryOwlDB = new SQLiteDatabase  
dim pantryOwlFile as FolderItem = FolderItem(SpecialFolder.Documents.Child(“Xojo”).Child(“pantry owl”).Child(“database”).Child(“pantryowl.sqlite”))

if not app.pantryOwlDB.AttachDatabase(pantryOwlFile, “db1”) then  
msgbox app.pantryOwlDB.ErrorMessage

end if[/code]

if I replace the code with this, I am able to connect to the database:

[code] dim dbFile As FolderItem = SpecialFolder.Documents.Child(“Xojo”).Child(“pantry owl”).Child(“database”).Child(“pantryowl.sqlite”)

app.pantryOwlDB = new SQLiteDatabase

If dbFile \<\> Nil And dbFile.Exists Then  
app.pantryOwlDB.DatabaseFile = dbFile

else  
msgbox “unable to connect to database file.”

End If[/code]

I am interested in attaching the database to perform comparisons with another sqlite database. Anyone come across this issue when attaching a sqlite database?

thank you in advance.

---

<div class="post-metadata">

**Author:** ![Bob\_Coleman](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@Bob\_Coleman](https://forum.xojo.com/u/Bob_Coleman)\
**Post date:** [April 28, 2015, 3:29pm UTC](https://forum.xojo.com/t/sqlite-operation-cannot-be-completed-because-the-database-is-closed/23906/2 "2015-04-28T15:29:14Z")

</div>

You have created app.pantryOwlDB in your code but you have not connected to it before trying to attach another database to it.

I have not used the AttachDatabase functionality before but that is what jumps out at me.

---

<div class="post-metadata">

**Author:** ![Jim\_Cramer](https://forum.xojo.com/user_avatar/forum.xojo.com/jim_cramer/32/2268_2.png) [@Jim\_Cramer](https://forum.xojo.com/u/Jim_Cramer)\
**Post date:** [April 28, 2015, 3:34pm UTC](https://forum.xojo.com/t/sqlite-operation-cannot-be-completed-because-the-database-is-closed/23906/3 "2015-04-28T15:34:12Z")

</div>

Bob is correct. You need to “Connect” before you “AttachDataBase”. Also, while technically not an error, the extra folder item is not needed in your first example:

```auto
dim pantryOwlFile as FolderItem = FolderItem(SpecialFolder.Documents.Child("Xojo").Child("pantry owl").Child("database").Child("pantryowl.sqlite"))
```

Can be changed to this:

```auto
dim pantryOwlFile as FolderItem = SpecialFolder.Documents.Child("Xojo").Child("pantry owl").Child("database").Child("pantryowl.sqlite")
```

---

<div class="post-metadata">

**Author:** ![Rich\_H](https://forum.xojo.com/user_avatar/forum.xojo.com/rich_h/32/18188_2.png) [@Rich\_H](https://forum.xojo.com/u/Rich_H)\
**Post date:** [April 28, 2015, 3:43pm UTC](https://forum.xojo.com/t/sqlite-operation-cannot-be-completed-because-the-database-is-closed/23906/4 "2015-04-28T15:43:35Z")

</div>

Bob & Jim,

thank you so much! connecting first before attaching the database did the trick!

I was following the example on the [attachdatabase docs](http://documentation.xojo.com/index.php/SQLiteDatabase.AttachDatabase) in which it did not indicate this.

Also, Jim, you are correct. the extra folderitem was not needed and have removed.

thank you both again.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:44pm UTC](https://forum.xojo.com/t/sqlite-operation-cannot-be-completed-because-the-database-is-closed/23906/5 "2020-10-29T17:44:17Z")

</div>


