How should I build a simple database package for my python application? -


I am creating database library for my application using sqlite3 as database. I want to mold it like this:

 db / __init__.py users.py blah.py etcpy 

so I will do it in Python:

  Import DB db.users.create ('username', 'password')  

I am about to deal with the database connection I am suffering from paralysis (oh no!) In fact, I do not want to use classes in these modules, it does not really seem appropriate that a "bunch of" user objects " Be able Ane who can manipulate a database of all the same way - so to get a connection to a no-go.

Do I have a global connection that uses all the modules, and then it is put in each module:

  # users.py Db_stuff import connection < / Code> 

Should I create a new connection for each module and he should survive?

Should I make a new connection for every transaction?

How should these database connections be used? The same cursor goes to the objects: can I create a new cursor for each transaction? Create only one for each database connection?

No, a connection does not spread on many modules - this is a poorly designed DB connection in the same class Take care and provide services to other sections / modules in your application.

This is not different from good design principles related to non-DB. A connection is a global resource, sharing that resource with multiple modules is equivalent to making a global variable accessible from many places - which is the default The form is a bad thing (unless you have a very attractive reason but you do not). Understand global resources in a classroom to control it.


Comments