Feb-22-2021, 03:19 AM
try the following. I couldn't test, so you may have to correct any errors.
import pyodbc def connect_db(driver, server, database, user, pwd): connect_str = f"DRIVER={driver};SERVER={server};PORT=1433;DATABASE={database};UID={user};PWD={pwd}" with pyodbc.connect(connect_str) as conn: with conn.cursor() as cursor: cursor.execute("SELECT TOP 3 name, collation_name FROM sys.databases") row = cursor.fetchone() while row: print (str(row[0]) + " " + str(row[1])) row = cursor.fetchone() def main(): azureServer = "pythonserver5874.database.windows.net" azureDB = "inventoryDatabase" userName = "lol" password = "lol" driver = "{ODBC Driver 17 for SQL Server}" connect_db(driver, azureServer, azureDB, userName, password) if __name__ == '__main__': main()