自动打开和关闭数据库连接
我正在我的应用程序中使用 SQLite3 编写一个用于数据库查询的类。该类的大多数方法与此非常相似:
def getPrice(self, symbol, date):
date = dt.datetime.strptime(date, '%Y-%m-%d')
conn = sqlite3.connect('stocks.db')
curs =conn.cursor()
curs.execute('''SELECT close FROM prices WHERE symbol = ? AND date = ?;''', (symbol, date))
close = curs.fetchall()
curs.close()
return close
唯一的区别是数据库查询和参数数量。是否有可能抽象数据库连接的打开和关闭?
我知道使用像 SQLAlchemy 这样的 ORM 可能会更容易。但我想了解一般如何解决此类问题,而不仅仅是与数据库相关的问题。
感谢您的建议!
编辑:这篇文章基本上回答了我的问题。
I am writing a class for database queries with SQLite3 in my application. Most of the methods of the class are very similar to this:
def getPrice(self, symbol, date):
date = dt.datetime.strptime(date, '%Y-%m-%d')
conn = sqlite3.connect('stocks.db')
curs =conn.cursor()
curs.execute('''SELECT close FROM prices WHERE symbol = ? AND date = ?;''', (symbol, date))
close = curs.fetchall()
curs.close()
return close
The only difference is the database query and the number of arguments. Is there a possibility to abstract the opening and closing of the database connection away?
I know that it would be probably easier to use a ORM like SQLAlchemy. But I want to understand how I solve this kind of problem in general, not only in relation to databases.
Thanks for your suggestions!
EDIT: This post basically answers my question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一的。您会对一种(而且只有一种)全球联系感到非常非常高兴。如果您在一个地方执行此操作,配置更改会容易得多。
其次,使用
with
语句和上下文管理器库。您的数据库模块如下所示:
这使您能够在一个地方更改数据库或数据库服务器技术。
First. You'll be much, much happier with one -- and only one -- global connection. Configuration changes are much easier if you do this in exactly one place.
Second, use the
with
statement and the context manager library.Your database module looks like this:
This gives you the ability to change databases, or database server technology in exactly one place.
请注意,从 Python2.6 开始,
sqlite.connect
返回上下文管理器:因此,不要用
contextlib.fitting
修饰连接——否则,您将失去提交/回滚行为,而只能获得< code>connection.close() 在退出with-statement
时调用。根据 PEP249:
因此提交/回滚行为比简单地有用得多呼叫关闭。
您可以使用 上下文管理器:
因为您有许多函数(例如
getPrice
)仅在 SQL 和参数上有所不同,您可以通过定义query
函数来减少重复的样板代码。您还可以定义一个上下文管理器来在出现错误时回滚连接并在退出 with 块时提交和关闭。可以在此处找到一个示例(对于 MySQL),将其调整为 sqlite3 应该不难..
参考:
Note that as of Python2.6,
sqlite.connect
returns a context manager:Therefore, do not decorate the connection with
contextlib.closing
-- otherwise, you will lose the commit/rollback behavior and instead only get theconnection.close()
called upon exiting thewith-statement
.Per PEP249:
So the commit/rollback behavior is much more useful than simply calling close.
You could use a context manager:
Since you have many functions like
getPrice
which differ only by the SQL and arguments, you could reduce the repetitious boiler-plate code by defining thequery
function.You could also define a context manager to rollback the connection on errors and commit as well as close upon exiting the with block. An example of this (for MySQL) can be found here, adapting it to sqlite3 should not be difficult..
Reference:
将该逻辑封装到一个对象中,将该对象传递给数据访问对象并要求它调用方法。
方面或装饰器可能是一种很好的做事方式。
您没有提到池化或交易。也想想那些。
Encapsulate that logic into an object, pass that object to the data access object and ask it to call the methods.
Aspects or decorators might be a good way to do things.
You don't mention pooling or transactions. Think about those as well.