PYODBC--Data source name not found and no default driver specified

后端 未结 15 1653
暖寄归人
暖寄归人 2020-11-28 13:01
import pyodbc
connection = pyodbc.connect(\'Driver = {SQL Server};Server=SIWSQL43A\\SIMSSPROD43A;\'
                            \'Database=CSM_reporting;Trusted_Conn         


        
相关标签:
15条回答
  • 2020-11-28 13:23

    I'm using

    Django 2.2

    and got the same error while connecting to sql-server 2012. Spent lot of time to solve this issue and finally this worked.

    I changed

    'driver': 'ODBC Driver 13 for SQL Server'

    to

    'driver': 'SQL Server Native Client 11.0'

    and it worked.

    0 讨论(0)
  • 2020-11-28 13:23

    I faced this issue and was looking for the solution. Finally I was trying all the options from the https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows , and for my MSSQL 12 only "{ODBC Driver 11 for SQL Server}" works. Just try it one by one. And the second important thing you have to get correct server name, because I thought preciously that I need to set \SQLEXPRESS in all of the cases, but found out that you have to set EXACTLY what you see in the server properties. Example on the screenshot:

    0 讨论(0)
  • 2020-11-28 13:24

    Try below:

    import pyodbc
    
    server = 'servername'
    
    database = 'DB'
    
    username = 'UserName'
    
    password = 'Password'
    
    cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
    
    cursor = cnxn.cursor()
    
    
    cursor.execute('SELECT * FROM Tbl')
    
    for row in cursor:
        print('row = %r' % (row,))
    
    0 讨论(0)
  • 2020-11-28 13:25

    Do not put a space after the Driver keyword in the connection string.

    This fails on Windows ...

    conn_str = (
        r'DRIVER = {SQL Server};'
        r'SERVER=(local)\SQLEXPRESS;'
        r'DATABASE=myDb;'
        r'Trusted_Connection=yes;'
    )
    cnxn = pyodbc.connect(conn_str)
    

    ... but this works:

    conn_str = (
        r'DRIVER={SQL Server};'
        r'SERVER=(local)\SQLEXPRESS;'
        r'DATABASE=myDb;'
        r'Trusted_Connection=yes;'
    )
    cnxn = pyodbc.connect(conn_str)
    
    0 讨论(0)
  • 2020-11-28 13:25

    Apart from the other answers, that considered the connection string itself, it might simply be necessary to download the correct odbc driver. My client just faced this issue when executing a python app, that required it. you can check this by pressing windows + typing "odbc". the correct driver should appear in the drivers tab.

    0 讨论(0)
  • 2020-11-28 13:25

    The below code works magic.

     SQLALCHEMY_DATABASE_URI = "mssql+pyodbc://<servername>/<dbname>?driver=SQL Server Native Client 11.0?trusted_connection=yes?UID" \
                                  "=<db_name>?PWD=<pass>"
    
    0 讨论(0)
提交回复
热议问题