我应该如何为我的 python 应用程序构建一个简单的数据库包?
我正在使用 sqlite3 作为基础为我的应用程序构建一个数据库库。我想像这样构造它:
db/ __init__.py users.py blah.py etc.py
所以我会用Python来做这个:
import db
db.users.create('username', 'password')
我正在遭受关于如何处理数据库连接的分析瘫痪(哦不!)。我真的不想在这些模块中使用类,能够创建一堆可以以相同方式操作同一个数据库的“用户”对象似乎并不合适——所以继承连接是不行。
我是否应该与所有模块使用的数据库建立一个全局连接,然后将其放入每个模块中:
#users.py
from db_stuff import connection
或者我应该为每个模块创建一个新连接并保持其活动状态?
或者我应该为每笔交易创建一个新连接?
这些数据库连接应该如何使用?游标对象也是如此:我是否为每个事务创建一个新游标?为每个数据库连接创建一个?
I'm building a database library for my application using sqlite3 as the base. I want to structure it like so:
db/ __init__.py users.py blah.py etc.py
So I would do this in Python:
import db
db.users.create('username', 'password')
I'm suffering analysis paralysis (oh no!) about how to handle the database connection. I don't really want to use classes in these modules, it doesn't really seem appropriate to be able to create a bunch of "users" objects that can all manipulate the same database in the same ways -- so inheriting a connection is a no-go.
Should I have one global connection to the database that all the modules use, and then put this in each module:
#users.py
from db_stuff import connection
Or should I create a new connection for each module and keep that alive?
Or should I create a new connection for every transaction?
How are these database connections supposed to be used? The same goes for cursor objects: Do I create a new cursor for each transaction? Create just one for each database connection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,不要将连接分布在多个模块上 - 这是糟糕的设计。让单个类处理数据库连接并向应用程序中的其他类/模块提供服务。
这与非数据库相关的良好设计原则没有什么不同。连接是一种全局资源。在许多模块上共享该资源类似于从许多地方访问全局变量 - 默认情况下这是一件坏事(除非您有非常令人信服的理由,但您没有)。将全局资源封装在一个类中来处理它。
No, don't spread a connection over several modules - this is bad design. Have a single class handle the DB connection and provide services to other classes/modules in your application.
This isn't different from non-DB-related good design principles. A connection is a global resource. Sharing that resource over many modules is akin to having a global variable accessible from many places - which is by default a bad thing (unless you have a very compelling reason, but you don't). Encapsulate the global resource in a class to handle it.
我知道这并不能真正回答您提出的实际问题,但真正的答案是您可能不应该实现自己的数据库包。您可能应该使用现有的库(例如 SQLALchemy),然后使用该库的标准模式。
如果您真的想自己做,那么最好的方法将取决于很多因素,例如,该项目是否确定只需要连接到一个数据库?
如果它是一个相当简单的应用程序,我认为导入全局连接对象可能是正确的方法。您始终可以在幕后将其替换为连接池等。
I know this doesn't really answer the actual question you asked, but the real answer is that you probably should not implement your own database package. You should probably use an existing one (e.g. SQLALchemy) and then use whatever pattern is standard for that library.
If you really want to do your own then the best approach is going to depend on a lot of factors, e.g. Is the project sure to only ever need a connection to one database?
If it's a fairly simple application I think importing a global connection object is probably the way to go. You can always swap it out for a connection pool behind the scenes, etc.