cx_Oracle 和数据源范例

发布于 2024-07-27 04:23:24 字数 587 浏览 1 评论 0原文

Java DataSource 中实现了一种用于数据库访问的 Java 范例。 该对象围绕数据库连接的创建创建了一个有用的抽象。 DataSource 对象保留数据库配置,但只会根据请求创建数据库连接。 这允许您将所有数据库配置和初始化代码保留在一个位置,并且可以轻松更改数据库实现或使用模拟数据库进行测试。

我目前正在开发一个使用 cx_Oracle 的 Python 项目。 在 cx_Oracle 中,直接从模块获取连接:

import cx_Oracle as dbapi
connection = dbapi.connect(connection_string)
# At this point I am assuming that a real connection has been made to the database.
# Is this true?

我试图找到与 cx_Oracle 中的 DataSource 并行的连接。 我可以通过创建一个新类并包装 cx_Oracle 来轻松创建它,但我想知道这是否是在 Python 中执行此操作的正确方法。

There is a Java paradigm for database access implemented in the Java DataSource. This object create a useful abstraction around the creation of database connections. The DataSource object keeps database configuration, but will only create database connections on request. This is allows you to keep all database configuration and initialization code in one place, and makes it easy to change database implementation, or use a mock database for testing.

I currently working on a Python project which uses cx_Oracle. In cx_Oracle, one gets a connection directly from the module:

import cx_Oracle as dbapi
connection = dbapi.connect(connection_string)
# At this point I am assuming that a real connection has been made to the database.
# Is this true?

I am trying to find a parallel to the DataSource in cx_Oracle. I can easily create this by creating a new class and wrapping cx_Oracle, but I was wondering if this is the right way to do it in Python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

锦上情书 2024-08-03 04:23:24

您可以通过查看 PEP-249 找到有关如何在 Python 中访问数据库的相关信息:Python数据库API规范v2.0cx_Oracle 符合此规范,许多 Python 数据库驱动程序也是如此。

在此规范中,Connection 对象表示数据库连接,但没有内置池。 SQLAlchemy 等工具确实提供了池化功能,尽管 SQLAlchemy 通常被称为 ORM,但它并没有就这样使用,并提供了在 SQL 引擎之上使用的良好抽象。

如果您确实想要进行对象关系映射,那么 SQLAlchemy 可以完成这项工作,您可以考虑它自己的声明性语法或其他层,例如 Elixir 位于 SQLAlchemy 之上,为更常见的用例提供了更高的易用性。

You'll find relevant information of how to access databases in Python by looking at PEP-249: Python Database API Specification v2.0. cx_Oracle conforms to this specification, as do many database drivers for Python.

In this specification a Connection object represents a database connection, but there is no built-in pooling. Tools such as SQLAlchemy do provide pooling facilities, and although SQLAlchemy is often billed as an ORM, it does not have to be used as such and offers nice abstractions for use on top of SQL engines.

If you do want to do object-relational-mapping, then SQLAlchemy does the business, and you can consider either its own declarative syntax or another layer such as Elixir which sits on top of SQLAlchemy and provides increased ease of use for more common use cases.

述情 2024-08-03 04:23:24

我认为在 Python 中没有一种“正确”的方法来做到这一点,除非更进一步,在你自己和数据库之间使用另一层。

根据想要使用 DataSource 概念(我只在 Java 中遇到过)的原因,SQLAlchemy(或类似的东西)可能会为您解决问题,而无需您从头开始编写一些东西。

如果这不符合要求,那么编写自己的包装器听起来是一个合理的解决方案。

I don't think there is a "right" way to do this in Python, except maybe to go one step further and use another layer between yourself and the database.

Depending on the reason for wanting to use the DataSource concept (which I've only ever come across in Java), SQLAlchemy (or something similar) might solve the problems for you, without you having to write something from scratch.

If that doesn't fit the bill, writing your own wrapper sounds like a reasonable solution.

黑凤梨 2024-08-03 04:23:24

是的,Python 有类似的抽象。

这是来自我们的本地构建回归测试,我们确保每当构建新的 python 时都可以与所有数据库进行对话。

if database == SYBASE:
    import Sybase
    conn = Sybase.connect('sybasetestdb','mh','secret')
elif database == POSTRESQL:
    import pgdb
    conn = pgdb.connect('pgtestdb:mh:secret')
elif database == ORACLE:
    import cx_Oracle
    conn = cx_Oracle.connect("mh/secret@oracletestdb")

curs=conn.cursor()
curs.execute('select a,b from testtable')
for row in curs.fetchall():
    print row

(注意,这是简单的版本,在我们的多数据库感知代码中,我们有一个 dbconnection 类,里面有这个逻辑。)

Yes, Python has a similar abstraction.

This is from our local build regression test, where we assure that we can talk to all of our databases whenever we build a new python.

if database == SYBASE:
    import Sybase
    conn = Sybase.connect('sybasetestdb','mh','secret')
elif database == POSTRESQL:
    import pgdb
    conn = pgdb.connect('pgtestdb:mh:secret')
elif database == ORACLE:
    import cx_Oracle
    conn = cx_Oracle.connect("mh/secret@oracletestdb")

curs=conn.cursor()
curs.execute('select a,b from testtable')
for row in curs.fetchall():
    print row

(note, this is the simple version, in our multidb-aware code we have a dbconnection class that has this logic inside.)

救星 2024-08-03 04:23:24

我只是吸收它并写了我自己的。 它允许我添加诸如抽象数据库(Oracle/MySQL/Access/等)、添加日志记录、事务回滚的错误处理等内容。

I just sucked it up and wrote my own. It allowed me to add things like abstracting the database (Oracle/MySQL/Access/etc), adding logging, error handling with transaction rollbacks, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文