Cannot establish connection to sql-server using pyodbc on Windows 7

bf1o4zei  于 2023-06-04  发布在  Windows
关注(0)|答案(7)|浏览(225)

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)')
hlswsv35

hlswsv351#

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)
qyzbxkaa

qyzbxkaa2#

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)
vkc1a9a2

vkc1a9a23#

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'

wn9m85ua

wn9m85ua4#

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.

u0njafvf

u0njafvf5#

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.

r1zk6ea1

r1zk6ea16#

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;')
whlutmcx

whlutmcx7#

First we need to be clear with the syntax-

cnxn = pyodbc.connect('DRIVER={SQL server};Server=server_name;DATABASE=database_name;Port=Port_Number;UID=username;PWD=password;')

Few Points-

  • Driver- You can check by printing print(pyodbc.drivers()) and from there you can pick.
  • Server- For server name you can check by running query select @@Servername
  • Database- For database you can check in your database to which you want to connect.
  • Port- Mostly for MS SQL Server it would be 1433.
  • UID- Consider key should be UID, don't change with other key I got failures due to this reason. Paste the user name.
  • PWD- Paste the password.

NOTE- Don't change the KEY otherwise it will give error.

And Trusted_Connection=yes is used for Windows Authentication.

相关问题