pyodbc connect to mysql code example
Example 1: pyodbc connect to sql server
import pyodbc
server = 'tcp:myserver.database.windows.net'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
Example 2: connect with pyodbc with statement
with pyodbc.connect(conx_string) as conx:
cursor = conx.cursor()
cursor.execute(query)
data = cursor.fetchall()