sqlite3 db open/close question

Posted by Renny on Sun 23 Nov 2014 03:38 PM — 2 posts, 12,759 views.

Canada #0
I am going by the documentation (http://www.gammon.com.au/scripts/doc.php?general=lua_sqlite3) and opening my db -> execute/query -> closing the database.

I do a lot of queries constantly so I am wondering if I should just be opening the database on plugin connect and then closing it on disconnect, instead of opening and closing thousands of times in a session.

Are there risks to not closing it? Does opening/closing so many times cause noticeable lag compared to just opening it once?

Any input appreciated.
Amended on Sun 23 Nov 2014 04:18 PM by Renny
Australia Forum Administrator #1
You should certainly only open and close it once per session.

There is a considerable overhead in opening a file.

As for risks, SQLite3 is specifically designed to cope with crashes. It has a rollback mechanism which restores any partially done transaction.

If you want to be extra safe, backup (make a copy) of the database file every day or so. Do this when the file is closed.