Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSSQL in python 2.7

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

like image 395
Shashi Avatar asked Sep 06 '11 08:09

Shashi


People also ask

Can I use Microsoft SQL Server with Python?

You can connect to a SQL Database using Python on Windows, Linux, or macOS.

Does SQL Server 2017 support Python?

SQL Server 2017 supports R and Python. revoscalepy is the primary library for scalable Python with functions for data manipulation, transformation, visualization, and analysis. It is an MSI setup downloader file.


1 Answers

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 
like image 88
codeape Avatar answered Sep 18 '22 23:09

codeape