質問

I'm using ActivePython 2.7.2.5 on Windows 7.

While trying to connect to a sql-server database with the pyodbc module using the below code, I receive the subsequent Traceback. Any ideas on what I'm doing wrong?

CODE:

import pyodbc
driver = 'SQL Server'
server = '**server-name**'
db1 = 'CorpApps'
tcon = 'yes'
uname = 'jnichol3'
pword = '**my-password**'

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=server;DATABASE=db1;UID=uname;PWD=pword;Trusted_Connection=yes')
cursor = cnxn.cursor()
cursor.execute("select * from appaudit_q32013")
rows = cursor.fetchall()
for row in rows:
    print row

TRACEBACK:

Traceback (most recent call last):
  File "pyodbc_test.py", line 9, in <module>
    cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=server;DATABASE=db1;UID=uname;PWD=pword;Trusted_Connection=yes')
pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53)')
役に立ちましたか?

解決

You're using a connection string of 'DRIVER={SQL Server};SERVER=server;DATABASE=db1;UID=uname;PWD=pword;Trusted_Connection=yes', you're trying to connect to a server called server, a database called db1, etc. It doesn't use the variables you set before, they're not used.

It's possible to pass the connection string parameters as keyword arguments to the connect function, so you could use:

cnxn = pyodbc.connect(driver='{SQL Server}', host=server, database=db1,
                      trusted_connection=tcon, user=uname, password=pword)

他のヒント

I had the same error message and in my case the issue was the [SQL Server] drivers required TLS 1.0 which is disabled on my server. Changing to the newer version of the SNAC, SQL Server Native Client 11.0 fixed the problem.

So my connection string looks like:

cnxn = pyodbc.connect(driver='{SQL Server Native Client 11.0}', 
                      host=server, database=db1, trusted_connection=tcon,
                      user=uname, password=pword)

I had faced this error due to another reason.
It was because my server had a "port" apart from the address.
I could fix that by assigning the following value to "Server" parameter of the connection string.

"...;Server=<server_name>,<port#>;..."

Note that it is a 'comma' and not 'colon'/'period'

Different security risks exist with either method. If you use Sql Server authentication you expose your userid/password in the code. But at least you process with the same credentials. If you use Windows authentication you have to insure all the possible users are setup with the right permission in the Sql server. With Sql authentication you can setup just one user but multiple people can use that one Sql User permissions wise.

I had the same issue today. I was using localhost in the connectionstring. Got rid of the issue by replacing localhost woth 'server name',. My db and application are running in the same machine.

If you don't have server name go to Sql server management studio and execute below query, which will give you the server name.

SELECT @@SERVERNAME

The connection string look as below

conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=myServerName;'
                      'Database=mydb;'
                      'Trusted_Connection=yes;')
cnxn = pyodbc.connect(driver='{SQL Server}', host=server, database=db1,
                       user=uname, password=pword)

print(cnxn)

I removed "Trusted_Connection" part and it worked for me.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top