How to connect Microsoft SQL server using Python ?

Submitted 4 years, 4 months ago
Ticket #315
Views 377
Language/Framework Python
Priority Low
Status Closed

Need some sudeo code to refer ....

Submitted on Dec 04, 20
add a comment

1 Answer

Verified

Here is the below which usually i prefer to do it,

Step 1:

You need to install the pyodbc package which will be used to connect Python to SQL Server.

pip install pyodbc

Step 2:

Get your SQL Server below details to connect,

                       'Server=server_name;'
                      'Database=database_name;'

Here is the sample code to connect,

import pyodbc 
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=server_name;'
                      'Database=database_name;'
                      'Trusted_Connection=yes;')

cursor = conn.cursor()
cursor.execute('SELECT * FROM database_name.table')

for row in cursor:
    print(row)

Submitted 4 years, 4 months ago


Latest Blogs