python query mssql

Solutions on MaxInterview for python query mssql by the best coders in the world

showing results for - "python query mssql"
Erika
07 Sep 2019
1import pyodbc
2server = '<server>.database.windows.net'
3database = '<database>'
4username = '<username>'
5password = '<password>'   
6driver= '{ODBC Driver 17 for SQL Server}'
7
8with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
9    with conn.cursor() as cursor:
10        cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid")
11        row = cursor.fetchone()
12        while row:
13            print (str(row[0]) + " " + str(row[1]))
14            row = cursor.fetchone()
15