In this tutorial, I will explain how you can connect to SQL Server 2008R2 using Python. As we all know Python is a very powerful language, you just need to know this language to play with different databases, excel,apps etc..

Let's do this...

First of all , you have to connect to database and for this you need credentials...

Open your SQL Server Management Studio, as soon as you open this, you will see a window just like this :

As I am connecting on my local machine, that's why using Windows Authentication.

Let's check a table in SSMS and later we will try to fetch all values of this table using Python.

It's coding time now.....

I am using default IDE here : IDLE for Python 2.7

 Some points to notice here :

  1. I am importing pymssql ( You can directly use it, you don't need to install anything)
  2. Next step is to create a connection, your password will be your windows login password if you are using Windows Authentication to connect to Sql Server.
  3. fetchone() returns rows one by one.

You can get more details about pymssql module in python official documentation.

© 2015 Techie's House. Designed by Bloggertheme9