Need help using a trusted connection to connect to SQL Server using SQLALCHEMY
So far I have this code to establish the connection
import sqlalchemy as sa
engine = sa.create_engine("mssql+pyodbc://REPORTING/REPORTING_System?driver={SQL Server}?TrustedConnection=yes")
view = f"SELECT Date From Table"
df = pd.read_sql_query(view, engine)
However I got this error message - DBAPIError: (pyodbc.Error) ('01S00', '[01S00] [Microsoft][ODBC Driver Manager] Invalid connection string attribute (0) (SQLDriverConnect)')
(Background on this error at: https://sqlalche.me/e/14/dbapi)
I tried to change the driver to {ODBC Driver 17 for SQL Server}
It did not help.
Related
I'm trying to connect to SQL Server remotely from my Linux (Debian) machine using Python, I tried everything but nothing has worked
import pyodbc
cnxn = pyodbc.connect('DRIVER={ODBC Driver 18 for SQL Server};'
'SERVER=192.168.108.186;'
'PORT=1433;DATABASE=teste;'
'UID=username;'
'PWD=my_password;'
'Trusted_Connection=no;'
'TrustServerCertificate=yes')
cursor = cnxn.cursor()
This gives me:
pyodbc.Error: ('HY000', '[HY000] [Microsoft][ODBC Driver 18 for SQL Server]SSPI
Provider: No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_1000)
(851968) (SQLDriverConnect)')
The same thing if I use pymssql
import pymssql
pymssql.connect(server=r'192.168.108.186', user=r'username', password=r'my_password', database=r'teste')
and this gives me
pymssql.OperationalError: (20002, b'DB-Lib error message 20002,
severity 9:\nAdaptive Server connection failed (192.168.108.186)\n
DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.108.186)\n')
for pyodbc.Error:
One of the reasons is an incorrect password. In that case, a Kerberos ticket is not created.
Also, as #AlwaysLearning said Trusted_Connection=yes; means to use the Windows Authentication, you have to go with either one of them.
You can see this link as well
https://techjogging.com/access-sqlserver-windows-trusted-connection-linux-python.html
I am having trouble with a MS SQL connection when using pyinstaller. When run in interactive mode, everything works as expected. After compiling to an exe, the MS SQL database connection times out on the first query with the following error:
(pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expire (0); ...
My connection string is similar to the following:
create_engine(
"mssql+pyodbc://USER:PASSWORD#SERVERIP/DB_NAME?driver=ODBC+Driver+17+for+SQL+Server"
)
In attempting to diagnose the issue, I am printing out the drivers available to pyodbc with pyodbc.drivers() (which shows a large disparity between available drivers in compiled vs interactive) as well as the driver in use using
print(session.bind.dialect.name)
> pyodbc
print(session.bind.dialect.driver)
> mssql
It returns the upper level python modules which are being used but not the .dll that is handling it at a lower level. Is there any way to find which exact driver is being used? Any tips on what could be causing the error in the compiled version in the firstplace would be appreciated as well.
The issue may be in your connection string.
To create a proper connection string to connect to MSSQL Server ODBC driver with sqlAlchemy use the following:
import urllib
from sqlalchemy import create_engine
server = 'serverName\instanceName,port' # to specify an alternate port
database = 'mydb'
username = 'myusername'
password = 'mypassword'
params = urllib.parse.quote_plus('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password)
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
Also, you can check the following article Connecting to Microsoft SQL Server using SQLAlchemy and PyODBC
Is there any way to find which exact driver [.dll] is being used?
import pyodbc
cnxn = engine.raw_connection()
print(cnxn.getinfo(pyodbc.SQL_DRIVER_NAME)) # msodbcsql17.dll
print(cnxn.getinfo(pyodbc.SQL_DRIVER_VER)) # 17.08.0001
I am trying to connect to MS SQL Server using pyodbc on local system and on connect to instance i get error:
[2020-06-21 15:39:04.110750]: ('08001', '[08001] [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. (-1) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 13 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (-1)')
My SQL Server is Express 2005 and try with drivers:
{ODBC Driver 13 for SQL Server}
{ODBC Driver 17 for SQL Server}
{SQL Native Client}
I test in sqlcmd with this command:
C:\Users\Moein>sqlcmd -S '.\Moein' -U 'sa' -P 'xxxx'
HResult 0xFFFFFFFF, Level 16, State 1
SQL Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections..
Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.
My Code :
import pyodbc
conn = pyodbc.connect(f'DRIVER={SQL Native Client};SERVER=.\Moein;DATABASE=Moein;UID=sa;PWD=xxxx',autocommit=True)
cursor = conn.cursor()
More:
Test -l 600 switch for login timeout more in sqlcmd > not answer recived
Test computername\instance and 127.0.0.1\instance > Not changed
Try full reinstall sql server on local > noting changed things
i found my problem
i use this connection string with pipe mode:
conn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};ServerName=Provider=SQLOLEDB.1;SERVER=.\testsv;DATABASE=test;Persist Security Info=False;UID=sa;PWD=xxxxx;Data Source=np:\\.\pipe\MSSQL$testsv\sql\query',autocommit=True)
My OS is Scientific Linux 7.5 and wants to connect to MS SQL server using python 2.7.5 i have configured unixODBC 2.3.1 and Microsoft ODBC Driver 13 for SQL Server
But when i use the following code:
import pyodbc
cnxn = pyodbc.connect(
'Driver={/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2};'
'Server=localhost;'
'User=sa;'
'Password=xxx;'
'Database=yyy;'
'Trusted_Connection=yes;')
i get the following error
pyodbc.Error: ('HY000', u'[HY000] [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]SSPI Provider: No Kerberos credentials available (default cache: KEYRING:persistent:1000) (851968) (SQLDriverConnect)')
and got the same error when used 'Driver={ODBC Driver 13 for SQL Server};'
if i use 'Driver={SQL Server};' i got stuck on following error
pyodbc.Error: ('01000', u"[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")
You should remove 'Trusted_Connection=yes;' from your connection string. It is used for Windows authentication, while you are trying to connect with SQL authentication (providing user name and password).
An alternate cause and solution, in case anyone ends up here for this reason:
If you're relying on an env file to populate the Username and Password components of the pyodbc connection string, and something goes wrong with your execution such that those values are not accessible, then you will get that No Kerberos credentials available error message.
Check the value of the connection string; in my case, the username and password were missing from the SQLAlchemy engine string when I printed it. Running my docker container with the proper --env-file= flag addressed this case.
I am trying to connect to a Microsoft SQL Server database running on a virtual machine in Microsoft Azure, from R.
Here is what my SQL server looks like
This is my connection string
library(RODBC)
channel = odbcConnect(dsn="something.cloudapp.net",uid="myusername",pwd="mypassword");
However, I keep getting this error
Warning messages:
1: In odbcDriverConnect("DSN=servername.cloudapp.net,1433;UID=myusername;PWD=mypassword") :
[RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
2: In odbcDriverConnect("DSN=servername.cloudapp.net,1433;UID=myusername;PWD=mypassword"):
ODBC connection failed
Why do I keep getting this error?
Try using the more flexible odbcDriverConnect function like this:
odbcDriverConnect('driver={SQL Server};Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;')
Here it is with carriage returns for readability:
odbcDriverConnect(
'driver={SQL Server};
Server=myServerAddress;
Database=myDataBase;
User Id=myUsername;
Password=myPassword;')
See also:
SQL Server RODBC Connection
http://www.connectionstrings.com/sql-server/
http://www.inside-r.org/packages/cran/rodbc/docs/odbcConnect