Python Code To Connect SQL Server

One way to connect Python to SQL Server is using pyodbc module. Here is a template that you can use:

import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=server_name;'
                      'Database=database_name;'
                      'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('SELECT * FROM table_name')
for i in cursor:
    print(i)

You need to replace server_name, database_name and table_name with your own values. You also need to install pyodbc module using pip install pyodbc command.

If you have any questions about this code, you can drop a line in comment.

Comments

Popular posts from this blog

Python chr() Built in Function

Stock Market Predictions with LSTM in Python

Collections In Python

Python Count Occurrence Of Elements

Python One Liner Functions