Unable to connect pyODBC with SQL Server 2008 Express R2

前端 未结 1 1886
春和景丽
春和景丽 2021-02-06 19:39

I am using following code to connect with SQL 2008 R2:

cnxnStoneedge = pyodbc.connect(\"DRIVER={SQL Server};SERVER=127.0.0.1;DATABASE=myDB;UID=admin;PWD=admin\")         


        
相关标签:
1条回答
  • 2021-02-06 20:15

    The following test code works for me to connect Python 2.7.5 with SQL Server 2008 R2 Express Edition:

    # -*- coding: utf-8 -*-
    import pyodbc
    
    connStr = (
        r'Driver={SQL Server};' +
        r'Server=(local)\SQLEXPRESS;' +
        r'Database=myDb;' +
        r'Trusted_Connection=Yes;'
        )
    
    db = pyodbc.connect(connStr)
    
    cursor1 = db.execute('SELECT [word] FROM [vocabulary] WHERE [ID]=5')
    
    while 1:
        row = cursor1.fetchone()
        if not row:
            break
        print row.word
    cursor1.close()
    db.close()
    

    and the following connection string also works for me because my \SQLEXPRESS instance is listening on port 52865:

    connStr = (
        r'Driver={SQL Server};' +
        r'Server=127.0.0.1,52865;' +
        r'Database=myDb;' +
        r'Trusted_Connection=Yes;'
        )
    
    0 讨论(0)
提交回复
热议问题