Using SQL Alchemy and pyodbc with IronPython 2.6.1
Asked Answered
M

4

1

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.

Melia answered 8/6, 2010 at 15:1 Comment(0)
A
2

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.

Ashleighashlen answered 9/6, 2010 at 15:39 Comment(0)
O
0

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.

Oulu answered 10/6, 2010 at 17:54 Comment(0)
B
0

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.

"""
Bozovich answered 14/6, 2010 at 19:12 Comment(0)
C
0

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.

Cylindroid answered 8/3, 2013 at 13:15 Comment(1)
Thanks for posting your answer! Please be sure to read the FAQ on Self-Promotion carefully. Also note that it is required that you post a disclaimer every time you link to your own site/product.Satsuma

© 2022 - 2024 — McMap. All rights reserved.