What's the simplest way to access mssql with python or ironpython?

不打扰是莪最后的温柔 提交于 2019-11-27 18:01:45

I use SQL Alchemy with cPython (I don't know if it'll work with IronPython though). It'll be pretty familiar to you if you've used Hibernate/nHibernate. If that's a bit too verbose for you, you can use Elixir, which is a thin layer on top of SQL Alchemy. To use either one of those, you'll need pyodbc, but that's a pretty simple install.

Of course, if you want to write straight SQL and not use an ORM, you just need pyodbc.

Everyone else seems to have the cPython -> SQL Server side covered. If you want to use IronPython, you can use the standard ADO.NET API to talk to the database:

import clr
clr.AddReference('System.Data')
from System.Data.SqlClient import SqlConnection, SqlParameter

conn_string = 'data source=<machine>; initial catalog=<database>; trusted_connection=True'
connection = SqlConnection(conn_string)
connection.Open()
command = connection.CreateCommand()
command.CommandText = 'select id, name from people where group_id = @group_id'
command.Parameters.Add(SqlParameter('group_id', 23))

reader = command.ExecuteReader()
while reader.Read():
    print reader['id'], reader['name']

connection.Close()

If you've already got IronPython, you don't need to install anything else.

Lots of docs available here and here.

ConcernedOfTunbridgeWells

pyodbc comes with Activestate Python, which can be downloaded from here. A minimal odbc script to connect to a SQL Server 2005 database looks like this:

import odbc

CONNECTION_STRING="""
Driver={SQL Native Client};
Server=[Insert Server Name Here];
Database=[Insert DB Here];
Trusted_Connection=yes;
"""

db = odbc.odbc(CONNECTION_STRING)
c = db.cursor()
c.execute ('select foo from bar')
rs = c.fetchall()
for r in rs:
    print r[0]

I also successfully use pymssql with CPython. (With and without SQLAlchemy).

http://adodbapi.sourceforge.net/ can be used with either CPython or IronPython. I have been very pleased with it.

PyPyODBC (http://code.google.com/p/pypyodbc) works under PyPy, Ironpython and CPython.

This article shows a Hello World sample of accessing mssql in Python.

PyPyODBC has almostly same usage as pyodbc, as it can been seen as a re-implemenation of the pyodbc moudle. Because it's written in pure Python, it can also run on IronPython and PyPy.

Actually, when switch to pypyodbc in your existing script, you can do this:

#import pyodbc               <-- Comment out the original pyodbc importing line

import pypyodbc as pyodbc    # Let pypyodbc "pretend" the pyodbc

pyodbc.connect(...)          # pypyodbc has 99% same APIs as pyodbc

...

I've used pymssql with standard python and liked it. Probably easier than the alternatives mentioned if you're just looking for basic database access.

Sample code.

If you are want the quick and dirty way with CPython (also works for 3.X python):

Install PYWIN32 after you install python http://sourceforge.net/projects/pywin32/files/pywin32/

Import the following library: import odbc

I created the following method for getting the SQL Server odbc driver (it is slightly different in naming depending on your version of Windows, so this will get it regardless):

def getSQLServerDriver():
    key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\ODBC\ODBCINST.INI")
    sqlServerRegExp =  re.compile('sql.*server', re.I | re.S)

    try:
        for i in range(0, 2048):
            folder = winreg.EnumKey(key, i)
            if sqlServerRegExp.match(folder):
                return folder.strip()
    except WindowsError:
        pass

Note: if you use the above function, you'll need to also import these two libraries: winreg and re

Then you use the odbc API 1 information as defined here: http://www.python.org/dev/peps/pep-0248/

Your connection interface string should look something like this (assuming you are using my above method for getting the ODBC driver name, and it is a trusted connection):

dbString = "Driver={SQLDriver};Server=[SQL Server];Database=[Database Name];Trusted_Connection=yes;".replace('{SQLDriver}', '{' + getSQLServerDriver() + '}')

This method has many down sides. It is clumsy because of only supporting ODBC API 1, and there are a couple minor bugs in either the API or the ODBC driver that I've run across, but it does get the job done in all versions of CPython in Windows.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!