简体   繁体   English

我应该如何为我的python应用程序构建一个简单的数据库包?

[英]How should I build a simple database package for my python application?

I'm building a database library for my application using sqlite3 as the base. 我正在使用sqlite3作为基础为我的应用程序构建数据库库。 I want to structure it like so: 我想像这样构造它:

db/
    __init__.py
    users.py
    blah.py
    etc.py

So I would do this in Python: 所以我会用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? 为每个数据库连接只创建一个?

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. 这与非DB相关的良好设计原则没有什么不同。 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. 将全局资源封装在类中以处理它。

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 (eg SQLALchemy) and then use whatever pattern is standard for that library. 您应该使用现有的(例如SQLALchemy),然后使用该库标准的任何模式。

If you really want to do your own then the best approach is going to depend on a lot of factors, eg 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. 您可以随时将其替换为幕后连接池等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何为python软件包编写一个简单的安装程序? - How should I write a simple installer for python package? 如何使用我的 Python3 fbs package package 一个 Sqlite3 数据库? - How do I package an Sqlite3 database with my Python3 fbs package? 如何使用 python -m 构建将 fonts 与我的 python package 一起分发? - How do I distribute fonts with my python package using python -m build? 通过这种简单的异常处理,我应该在哪里打印输出?(Python) - Where should I print my output at this simple exception handling?(Python) 我应该为PySIde应用程序使用单个数据库连接吗? - Should I use a single database connection for my PySIde application? 我应该如何继续在Python中使用银行应用程序 - How should i proceed with banking Application in Python 如何在我的python应用程序中添加简单的非交互式GUI? - How can I add a simple non-interactive gui to my python application? Python sqlite3-真正让我不知道如何构造数据库表 - Python sqlite3 - Truly at a loss at how I should structure my database tables 我应该用我的Python应用程序捆绑C库吗? - Should I bundle C libraries with my Python application? 如何构建我的Python包? - How do I structure my Python package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM