I tried to connect to a remote MSSQL server by using pymssql package but then I get "ImportError: No module named pymssql".
Yes, I have done a pip install pymssql which worked. I also looked into installing Cython but it seems such install would give more headaches, so not worth to attempt with such. If I am not totally wrong, pymssql recuires Cython.
The OS I am using is Windows 10 Professional.
Remote connection to MSSQL does work, since at the other side of the office they are connected to the MSSQL server by software Qlik. The MSSQL server firewall does not block port 1433.
So, since pymssql seems not to work out of the box, I have tried to import pyodbc, which in this case gives some error message and does not connect. To this point I tried different solutions listed here on SE and none worked so far.
Following appears so far to be the most successful approach:
import pyodbc
con = pyodbc.connect('DRIVER={SQL Server};SERVER=gxn.database.windows.net;DATABASE=instantcopy;UID=testdb#gxn;PASSWORD=verysecret')
cur = con.cursor()
con.close()
The error is:
pyodbc.Error: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'testdb'.
(18456) (SQLDriverConnect); [01S00] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0); [28000]
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'testdb'. (18456); [01S00] [Microsoft][OD
BC SQL Server Driver]Invalid connection string attribute (0)")
Any ideas?
Update1:
Using following correction.. I got this line from Azure suggesting following connection string:
Driver={SQL Server Native Client 11.0};Server=tcp:xxx.database.windows.net,1433;Database=xxx-instantorcopy;Uid=xxxtestdb#xxx;Pwd={your_password_here};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;
Solved:
The password was incorrect. So, if anyone is to attempt to connect to mssql I can tell you that I failed with 3 connectors and finally made it with pyodbc.
Related
I am getting a strange error with Python condo environments and PyODBC. I have multiple Conda environments, I am able to connect from one of the environment (from a Linux machine) to SQL Server hosted on Azure but not from the other one. Both Python environments have version 3.7.7 of Python and version 4.0.0 of Pyodbc. Code is exactly the same and connection string uses SQL Server 17 driver.
conn_str='DRIVER={ODBC Driver 17 for SQL Server};SERVER=' +server+';Authentication=ActiveDirectoryPassword;DATABASE='+database+';UID='+self.user_name+';PWD='+self.password
Error is :
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
As you can see above, the issue might be DNS-related. In the
connection string, try using an IP address instead of the hostname,
or check your DNS setup.
Try downgrade to ODBC driver 13 and have a try. For that version 17
needs to be uninstalled.
Try using connect from SQL Authentication instead of Active Directory
Password.
The SQL Server ODBC drivers for Linux from Microsoft are unable to determine instance names.
I am trying to connect to my database on MS SQL Server 2016 using pyodbc via the below python script from my laptop (on Windows 10) and planning to have the code deployed in a Linux RHEL 6.4 server.
conn=pyodbc.connect('Driver={SQL Server};'
'Server=DB_Instance;'
'Database=DB_Name;'
'UID=user_name;'
'PWD=password;'
'Trusted_Connection=no;');
At my laptop, SQL Server (version: 10.00.17763.01) and SQL Server Native Client 11.0 (version: 2011.110.7493.04) are already available.
While executing the python script from my laptop, I am getting the below error message.
pyodbc.operationalError: ('08001', '[08001] [Microsoft] [ODBC SQL Server Driver][DBNETLIB]SSL Security error (18) (SQLDriverConnect); [08001] [Microsoft] [ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (SECDoClientHandshake()). (772)')
As per the organization standard, TLS 1.0 is disabled on the windows server where the SQL Server is installed in the network. Since I am accessing the database via python script, we cannot temporarily enable TLS 1.0. I am looking for a direction. Any help is greatly appreciated!!
Gord Thompson pointed Guna in the right direction in the comments:
Can you try using ODBC Driver 17 for SQL Server and see if that works for you?
– Gord Thompson
Guna said that this worked:
I was facing different set of errors. While adding the port number in addition to the server name, the problem got resolved. The ODBC Driver name also needs to be updated. conn=pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};' 'Server=DB_Instance,port;' 'Database=DB_Name;' 'UID=user_name;' 'PWD=password;' 'Trusted_Connection=no;');
– Guna
Posting this as a community Wiki just so that any searchers can see quickly that it was answered.
PROBLEM: -
When trying to perform password change for MSSQL server, you get the following error message:-SSL Security error
SOLUTION: -
It's likely an issue with the version of the SQL Native Client driver being used to establish the connection.
Please Download & install "ODBC Driver 13 for SQL Server" on the CPM servers.
https://www.microsoft.com/en-us/download/confirmation.aspx?id=50420
Download 32 bit driver : x86\msodbcsql.msi
Install as usual
Edit MSSQL Platforms to change the connection command from
Driver={SQL Server};Server=%ADDRESS%;Database=%DATABASE%;Uid=%USER%;Pwd=%LOGONPASSWORD%;
to
Driver={ODBC Driver 13 for SQL Server};Server=%ADDRESS%;Database=%DATABASE%;Uid=%USER%;Pwd=%LOGONPASSWORD%;
Restart the CPM service to pick up the platform changes & retest
I am trying to connect to MS Sql Server using python and sqlalchemy, with trusted connection / active directory / kerberos authentication. I was able to connect before, but now I am getting this error:
Error: ('HY000', u'[HY000] [Microsoft][ODBC Driver 13 for SQL Server]SSPI Provider: Ticket expired (negative cache) (851968) (SQLDriverConnect)')
I was able to resolve this by running kinit in terminal with no options.
I'm sure this is a stupidly simple issue but I just can't sort it out. I'm trying to open a local database with pyodbc but it won't let me.
The database and script are on the same path, there are no passwords. I only have one installation of Python on my system - Anaconda 3, and this is where pyodbc installed itself when I used PIP to install the Python 3.5, 64 bit whl file from Christoph Gohlke
import pyodbc
pyodbc.connect('Driver={SQL Server};Server=(local);Database=tblGrid.mdb;Trusted_Connection=yes;')
I get the following error:
pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][Shared Memory]SQL Server does not exist or access denied. (17) (SQLDriverConnect)')
Thanks
You need to use the Microsoft Access ODBC driver to open an *.mdb file.
Here is an example of connecting:
pyodbc.connect(
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\some\path\tblGrid.mdb')
Is it possible connect using local Domain credentials that I use to log into my OSX System to connect to a SQLServer DB with python, pyodbc, unixodbc, and FreeTDS like I would when connecting from a Windows system?
Windows Connection String that works:
pyodbc.connect('Trusted_Connection=yes', driver='FreeTDS', TDS_Version = 7.0, server='<SERVER>', port=<PORT>, database='<DATABASE>')
When I attempt this from a Mac I get the following error:
pyodbc.Error: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
I am attempting to avoid hard coding usernames and passwords.
The Trusted_Connection setting indicates whether to use Windows Authentication Mode for login validation or not. Given that you're using a Mac, I suspect that Windows Authentication Mode will not be possible and so it will be necessary to pass the username and password to the connection string.