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 float() Built in Function

Python String encode() Method

Python str() Built in Function

Python String format_map() Method

Python String format() Method