I am using peewee to access a SQLite DB.
I have made a model.py
like:
from peewee import *
db = SqliteDatabase('people.db')
class Person(Model):
name = CharField()
birthday = DateField()
is_relative = BooleanField()
class Meta:
database = db
In another Python file (with import model
) I then manipulate the DB with calls like Person.create()
or Person.select(name=='Joe').delete_instance()
.
The Quickstart says at the end to call db.close()
to close the connection. Does this apply to my case as well? Am I supposed to call something like model.db.close()
?
According to Charles Leifer, the maker of peewee it is the programmer's job to terminate connections. The documentation about Connection Pools tell, that all connections are thread-local, so as long as the Model is in use, the connection stays open and dies, if the thread containing the Transaction joins the Main-Thread.
Charles explicitly answers a question about the Connection Pool. The answer is a bit generalized, but I suppose this applies to all connections equally: About connection pool
Implicit answers on the topic:
Error 2006: MySQL server has gone away
Excerpt from the docs Quickstart Page:
Although it’s not necessary to open the connection explicitly, it is good practice since it will reveal any errors with your database connection immediately, as opposed to some arbitrary time later when the first query is executed. It is also good to close the connection when you are done – for instance, a web app might open a connection when it receives a request, and close the connection when it sends the response.
The final answer to your question is, based on these information: No.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With