与sql server的python连接

时间:2019-01-21 13:57:08

标签: python sql-server pyodbc

cnxn = pyodbc.connect('DRIVER={ODBC Driver 11 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

pyodbc.InterfaceError

  

('IM002','[IM002] [Microsoft] [ODBC驱动程序管理器]数据源名称   找不到,也没有指定默认驱动程序(0)(SQLDriverConnect)')

1 个答案:

答案 0 :(得分:0)

这应该可以正常工作:

import pytds 

SERVER.IP = 'xx.xxx.xxx.xxx'
SERVER.DB_NAME = 'xxxxxx'
SERVER.USERNAME = 'xxxx'
SERVER.PWD = 'xxxxxx'

   with pytds.connect(SERVER.IP, SERVER.DB_NAME, SERVER.USERNAME, SERVER.PWD) as conn:
        cursor = conn.cursor()
        if conn == False:
            print("Error, Could not connect to the database")
        else:
            print("Connected to DB")