将 SQL Alchemy 和 pyodbc 与 IronPython 2.6.1 结合使用

发布于 2024-09-04 17:57:11 字数 2099 浏览 1 评论 0原文

我正在使用 IronPython 和 clr 模块通过 SMO 检索 SQL Server 信息。我想使用 SQL Alchemy 在 SQL Server 数据库中检索/存储这些数据,但在加载 pyodbc 模块时遇到一些问题。

设置如下:

  • IronPython 2.6.1(安装在 D:\Program Files\IronPython)
  • CPython 2.6.5(安装在 D:\Python26)
  • SQL Alchemy 0.6.1(安装在 D:\Python26\Lib\site-packages\ sqlalchemy)
  • pyodbc 2.1.7 (安装在 D:\Python26\Lib\site-packages)

我在 IronPython site.py 中有这些条目来导入 CPython 标准和第三方库:

# Add CPython standard libs and DLLs
import sys
sys.path.append(r"D:\Python26\Lib")
sys.path.append(r"D:\Python26\DLLs")
sys.path.append(r"D:\Python26\lib-tk")
sys.path.append(r"D:\Python26")

# Add CPython third-party libs
sys.path.append(r"D:\Python26\Lib\site-packages")

# sqlite3
sys.path.append(r"D:\Python26\Lib\sqlite3")

# Add SQL Server SMO
sys.path.append(r"D:\Program Files\Microsoft SQL Server\100\SDK\Assemblies")
import clr
clr.AddReferenceToFile('Microsoft.SqlServer.Smo.dll')
clr.AddReferenceToFile('Microsoft.SqlServer.SqlEnum.dll')
clr.AddReferenceToFile('Microsoft.SqlServer.ConnectionInfo.dll')

SQL Alchemy 在 IronPython 中导入正常,让我收到尝试连接到 SQL Server 时出现此错误消息:

IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.3607
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> e = sqlalchemy.MetaData("mssql://")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python26\Lib\site-packages\sqlalchemy\schema.py", line 1780, in __init__
  File "D:\Python26\Lib\site-packages\sqlalchemy\schema.py", line 1828, in _bind_to
  File "D:\Python26\Lib\site-packages\sqlalchemy\engine\__init__.py", line 241, in create_engine
  File "D:\Python26\Lib\site-packages\sqlalchemy\engine\strategies.py", line 60, in create
  File "D:\Python26\Lib\site-packages\sqlalchemy\connectors\pyodbc.py", line 29, in dbapi
ImportError: No module named pyodbc

此代码在 CPython 中运行良好,但看起来无法从 IronPython 访问 pyodbc 模块。

有什么建议吗?我意识到这可能不是解决问题的最佳方法,因此我愿意以不同的方式解决这个问题。只是想获得一些使用 SQL Alchemy 和 pyodbc 的经验。

I'm using IronPython and the clr module to retrieve SQL Server information via SMO. I'd like to retrieve/store this data in a SQL Server database using SQL Alchemy, but am having some trouble loading the pyodbc module.

Here's the setup:

  • IronPython 2.6.1 (installed at D:\Program Files\IronPython)
  • CPython 2.6.5 (installed at D:\Python26)
  • SQL Alchemy 0.6.1 (installed at D:\Python26\Lib\site-packages\sqlalchemy)
  • pyodbc 2.1.7 (installed at D:\Python26\Lib\site-packages)

I have these entries in the IronPython site.py to import CPython standard and third-party libraries:

# Add CPython standard libs and DLLs
import sys
sys.path.append(r"D:\Python26\Lib")
sys.path.append(r"D:\Python26\DLLs")
sys.path.append(r"D:\Python26\lib-tk")
sys.path.append(r"D:\Python26")

# Add CPython third-party libs
sys.path.append(r"D:\Python26\Lib\site-packages")

# sqlite3
sys.path.append(r"D:\Python26\Lib\sqlite3")

# Add SQL Server SMO
sys.path.append(r"D:\Program Files\Microsoft SQL Server\100\SDK\Assemblies")
import clr
clr.AddReferenceToFile('Microsoft.SqlServer.Smo.dll')
clr.AddReferenceToFile('Microsoft.SqlServer.SqlEnum.dll')
clr.AddReferenceToFile('Microsoft.SqlServer.ConnectionInfo.dll')

SQL Alchemy imports OK in IronPython, put I receive this error message when trying to connect to SQL Server:

IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.3607
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> e = sqlalchemy.MetaData("mssql://")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python26\Lib\site-packages\sqlalchemy\schema.py", line 1780, in __init__
  File "D:\Python26\Lib\site-packages\sqlalchemy\schema.py", line 1828, in _bind_to
  File "D:\Python26\Lib\site-packages\sqlalchemy\engine\__init__.py", line 241, in create_engine
  File "D:\Python26\Lib\site-packages\sqlalchemy\engine\strategies.py", line 60, in create
  File "D:\Python26\Lib\site-packages\sqlalchemy\connectors\pyodbc.py", line 29, in dbapi
ImportError: No module named pyodbc

This code works just fine in CPython, but it looks like the pyodbc module isn't accessible from IronPython.

Any suggestions? I realize that this may not be the best way to approach the problem, so I'm open to tackling this a different way. Just wanted to get some experience with using SQL Alchemy and pyodbc.

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

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

发布评论

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

评论(4

时光清浅 2024-09-11 17:57:11

pyodbc 很可能与 IronPython 不兼容,因为它是为与 cPython 一起使用而设计的。

IronPython 当然内置了某种 ODBC(实际上,ADO.net 似乎就是这样)兼容性,但 DBAPI 将是让 SQLAlchemy 使用它的最直接方法。

下面是一些特定于 MS 的非 DBAPI 示例: http://www.ironpython.info/index .php/Accessing_SQL_Server
有人在 2006 年谈论 DBAPI: http:// /hex-dump.blogspot.com/2006/10/ironpython-and-adonet-part-2.html
更新一些: http://bitbucket.org/jdhardy/adonet-dbapi/

它表明微软在 IronPython 上投入了很多钱,但在兼容的 DBAPI 驱动程序上投入的钱却为零。

its very likely that pyodbc is not compatible with IronPython, as it was designed for usage with cPython.

IronPython certainly has some kind of ODBC (actually, ADO.net seems like where its at) compatibility built into it, but a DBAPI would be the most direct way to get SQLAlchemy working with it.

So here's some MS-specific non-DBAPI example: http://www.ironpython.info/index.php/Accessing_SQL_Server
someone talking about DBAPI in 2006: http://hex-dump.blogspot.com/2006/10/ironpython-and-adonet-part-2.html
something a little more recent: http://bitbucket.org/jdhardy/adonet-dbapi/

It says something that MS pours however much money into IronPython but zero into a compliant DBAPI driver.

南城追梦 2024-09-11 17:57:11

您可以尝试使用 SQLAlchemy 的 adodbapi 支持;最新版本的adodbapi (2.3.0) 支持IronPython。

您只需确保 adodbapi 包位于 sys.path 上,然后在连接字符串中使用“mssql+adodbapi://”而不是“mssql://”。

You could try using SQLAlchemy's adodbapi support instead; the latest version of adodbapi (2.3.0) supports IronPython.

You should only have to make sure the adodbapi package is on sys.path, and then use 'mssql+adodbapi://' instead of 'mssql://' in your connection string.

写给空气的情书 2024-09-11 17:57:11

adodbapi 似乎是可行的方法,但这里是 adodbapi.py 中的一个片段,它随 SQL Alchemy 一起提供在 dialects 文件夹下

"""
The adodbapi dialect is not implemented for 0.6 at this time.

"""

adodbapi seems the way to go, but here's a snippet from adodbapi.py that ships with SQL Alchemy under the dialects folder

"""
The adodbapi dialect is not implemented for 0.6 at this time.

"""
牵强ㄟ 2024-09-11 17:57:11

SQLAlchemy 无法直接在 IronPython 下运行,因为 pyodbc 目前与 IronPython 不兼容。

不过,您可以在 IronPython 下使用 pypyodbc 作为 dbi-2.0 投诉库,这与 pyodbc 类似,并允许在 Ironpython 下运行 sqlalchemy,此操作方法介绍了 4 个步骤启用它。

免责声明:我是 pypyodbc 的维护者。

SQLAlchemy can not directly run under IronPython, because pyodbc currently is not compatible with IronPython.

However, you can use pypyodbc under IronPython as a dbi-2.0 complaint library, which is similar to pyodbc,and enables running sqlalchemy under Ironpython, this How-to describes the 4 steps to enable it.

Disclaimer: I'm the maintianer of pypyodbc.

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