Skip to content

Instantly share code, notes, and snippets.

@benyaminl
Created March 11, 2022 05:19
Show Gist options
  • Save benyaminl/eccf04ce5cb71eb3a25a6587b7781a5b to your computer and use it in GitHub Desktop.
Save benyaminl/eccf04ce5cb71eb3a25a6587b7781a5b to your computer and use it in GitHub Desktop.
Python ODBC connect to SQL Server 2008 from Windows 10 21H2
import pyodbc
# Specifying the ODBC driver, server name, database, etc. directly
con = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=127.0.0.1;PORT=1433;DATABASE=coba;UID=sa;PWD=12345')
# Using a DSN, but providing a password as well
# cnxn = pyodbc.connect('DSN=test;PWD=password')
# Create a cursor from the connection
db = con.cursor()
db.execute("SELECT @@version;")
row = db.fetchone()
while row:
print(row[0])
row = db.fetchone()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment