Skip to content

Instantly share code, notes, and snippets.

@KentaYamada
Created December 30, 2015 12:41
Show Gist options
  • Save KentaYamada/16e020166aa38ecae2c3 to your computer and use it in GitHub Desktop.
Save KentaYamada/16e020166aa38ecae2c3 to your computer and use it in GitHub Desktop.
Connect MSSQL(ODBC)
#-*- coding utf-8 -*-
import pyodbc
def test():
connectString = "DRIVER={SQL SERVER}; SERVER=(local)\SQLEXPRESS;DATABASE=;TrustedConnction=yes;"
sql = "select sysdb.name from sysdatabases sysdb"
#connect server
with pyodbc.connect(connectString) as conn:
cursor = conn.cursor()
cursor.execute(sql)
for row in cursor.fetchall():
#Output db name
print("Name:{0}".format(row))
if __name__ =="__main__":
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment