I’m using pyodbc to connect to the database.
Replace the serverName, databaseName and sql variable values with ones specific to your data.
import pyodbc serverName = "TestInstance" databaseName = "TestDB" sql = "SELECT * FROM dbo.TestUser" connectionString = "Driver={{SQL Server}};Server={0};Database={1};Trusted_Connection=yes;" conn = pyodbc.connect(connectionString.format(serverName, databaseName)) cursor = conn.cursor() cursor.execute(sql) for row in cursor: print(row)
For each row, we’ll get a tuple, like (1,true).
This example used Windows Authentication to connect. If we want to use SQL Authentication, substitute ‘Trusted_Connection=yes’ with ‘UID=UserName;PWD=pass1’, substituting UserName and pass1 with your values.