Tuesday, December 22, 2020

py0021 : Make Python connection with SQLServer database

Below is the code which can be used for to make connection with SQLServer database. Make sure python have already imported with pyodbc driver. If you have missing driver, use below command to install driver.

>pip install pyodbc

If you faced below message in output that means driver already installed
"Requirement already satisfied: pyodbc in c:\users\******\appdata\local\programs\python\python37-32\lib\site-packages (4.0.25)"

Code : 
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=IPAddress;'
                      'Database=databaseName;'
                      'username=sa;'
                      'password=dbpASSWORD;'
                      )
cursor = conn.cursor()
cursor.execute('SELECT * FROM information_schema.tables')
for row in cursor:
print(row)

No comments:

Post a Comment

web stats