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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With