41

Is there a module available for connection of MSSQL and python 2.7?

I downloaded pymssql but it is for python 2.6. Is there any equivalent module for python 2.7?

I am not aware of it if anyone can provide links.


Important note: in the meantime there is a pymssql module available. Don't miss to read the answer at the end of this page: https://stackoverflow.com/a/25749269/362951

Community
  • 1
  • 1
Shashi
  • 2,137
  • 3
  • 22
  • 37
  • Isn't 2.7 backwards compatible with 2.6? i.e. doesn't that module work in 2.6? – rplnt Sep 06 '11 at 08:48
  • No while installing http://code.google.com/p/pymssql/downloads/detail?name=pymssql-1.9.908.win32-py2.6.exe&can=2&q= it check for python version and end the setup – Shashi Sep 06 '11 at 08:51
  • 2
    There are snapshots for 2.7 http://build.damoxc.net/downloads/pymssql/snapshots/ ...if that helps. – rplnt Sep 06 '11 at 08:53
  • I found one repository of python pakg hope it will usefull for all http://www.lfd.uci.edu/~gohlke/pythonlibs/ – Shashi Dec 20 '12 at 06:52

4 Answers4

58

You can also use pyodbc to connect to MSSQL from Python.

An example from the documentation:

import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=me;PWD=pass')
cursor = cnxn.cursor()
cursor.execute("select user_id, user_name from users")
rows = cursor.fetchall()
for row in rows:
    print row.user_id, row.user_name

The SQLAlchemy library (mentioned in another answer), uses pyodbc to connect to MSSQL databases (it tries various libraries, but pyodbc is the preferred one). Example code using sqlalchemy:

from sqlalchemy import create_engine
engine = create_engine("mssql://me:pass@localhost/testdb")
for row in engine.execute("select user_id, user_name from users"):
    print row.user_id, row.user_name
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
codeape
  • 97,830
  • 24
  • 159
  • 188
17

If you're coming across this question through a web search, note that pymssql nowadays does support Python 2.7 (and 3.3) or newer. No need to use ODBC.

From the pymssql requirements:

Python 2.x: 2.6 or newer. Python 3.x: 3.3 or newer.

See http://pymssql.org/.

SaeX
  • 17,240
  • 16
  • 77
  • 97
  • From the MSFT python MSSQL driver page ( https://learn.microsoft.com/en-us/sql/connect/python/python-driver-for-sql-server?view=sql-server-2017 ) "There are several python SQL drivers available. However, Microsoft places its testing efforts and its confidence in pyodbc driver. " I doubt this was the case in 2014 though:) – riffrazor Oct 23 '18 at 22:47
5

Install pyodbc using pip as follows: pip install pyodbc

import pyodbc
cnxn = pyodbc.connect("DRIVER={SQL Server};SERVER=SOME-PC;DATABASE=my_db")
cursor = cnxn.cursor()


cursor.execute("insert into test_tb values(6, 'name')")

cursor.execute("select id, name from my_tb")
rows = cursor.fetchall()
for row in rows:
    print row.id, row.name

For details, see

https://github.com/mkleehammer/pyodbc/wiki

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
akila
  • 51
  • 1
  • 1
3

You can try out SQLAlchemy: The SQLAlchemy Object Relational Mapper presents a method of associating user-defined Python classes with database tables, and instances of those classes (objects) with rows in their corresponding tables.

You can refer following links: 1> http://www.sqlalchemy.org/docs/ 2> http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html

avasal
  • 14,350
  • 4
  • 31
  • 47