views:

3208

answers:

6

I've got mssql 2005 running on my personal computer with a database I'd like to run some python scripts on. I'm looking for a way to do some really simple access on the data. I'd like to run some select statements, process the data and maybe have python save a text file with the results.

Unfortunately, even though I know a bit about python and a bit about databases, it's very difficult for me to tell, just from reading, if a library does what I want. Ideally, I'd like something that works for other versions of mssql, is free of charge and licensed to allow commercial use, is simple to use, and possibly works with ironpython.

+8  A: 

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.

Jason Baker
Pyodb works for most tasks
Harald Scheirich
+2  A: 

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

Ali A
For the record, I believe that pymssql support in SQLAlchemy is deprecated. The recommended driver is pyodbc (that's not to say that pymssql won't work standalone).
Jason Baker
Oh, thanks for this.
Ali A
A: 

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.

Rizwan Kassim
+5  A: 

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]
ConcernedOfTunbridgeWells
I am a major fan of Pyodbc. It is extremely simple and easy to use with sql server and many other data sources as well.
TimothyAWiseman
+8  A: 

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.

wilberforce
That example seems a bit bloated.
graffic
Unfortunately, ADO.NET is a bit more verbose than the corresponding Python DBAPI code (and this is true of the .NET class library in general). But it's not too bad, and easy enough to wrap in a DBAPI module. I've done this for working with SQLAlchemy.
wilberforce
Very good point, but don't forget that SMO is another option.
TimothyAWiseman
+3  A: 

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

dwelden