Inserting Rows to SQL SERVER using pyodbc - sql-server

I am having terrible time connecting to SQL Server using pyodbc from the linux machine (Ubuntu 16.04).
conn = pyodbc.connect(r'DRIVER={FreeTDS};PORT=**; SERVER=**; DATABASE=**;UID=AA;PWD=hfghj;')
curr = conn.cursor()
curr.fast_executemany = True
query = "INSERT INTO dbo.STG_CONTACTABILITY_SCORE VALUES (?" + ",?"*21 + ")"
sql_data = list(map(tuple, i.values))
curr.executemany(query, sql_data)
I am getting following error:
('HY004', '[HY004] [FreeTDS][SQL Server]Invalid data type (0)
(SQLBindParameter)')
The same insert query works from my windows laptop when I change the connection string to:
conn = pyodbc.connect(r'DRIVER={SQL Server};PORT=**; SERVER=**; DATABASE=**; UID=AA; PWD=hfghj;')
If I change the Driver from FreeTDS to SQL SERVER on my Linux machine
I get the following error:
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open
lib 'SQL Server' : file not found (0) (SQLDriverConnect)")
I searched for answers but there isn't much out there that could solve mine problem.
What is the driver to connect to SQL SERVER from Linux machine?

What is the driver to connect to SQL SERVER from Linux machine?
If you install Microsoft's ODBC driver as described in
Installing the Microsoft ODBC Driver for SQL Server on Linux and macOS
then you can use DRIVER=ODBC Driver 17 for SQL Server or DRIVER=ODBC Driver 13 for SQL Server depending on which version you choose.

Related

Which exact driver is sqlalchemy using?

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

Issues connecting to Microsoft SQL Server Express using pyodbc on a Ubuntu 20.10 machine [duplicate]

I am trying to connect to MS SQL Server using pyodbc from a remote machine running Ubuntu 16.04.
import pyodbc
conn = pyodbc.connect(r'DRIVER=ODBC Driver 17 for SQL Server; SERVER=xxxTest-SRV; PORT=51333; DATABASE=TestDB; UID=xxxx; PWD=xxxx;')
I'm getting following error:
pyodbc.OperationalError: ('HYT00', '[HYT00] [unixODBC][Microsoft][ODBC
Driver 17 for SQL Server]Login timeout expired (0)
(SQLDriverConnect)')
I tried using the server IP in the connection string but still no luck.
However, I am able to connect to using sqlcmd from the terminal
Following works:
sqlcmd -S xxxTest-SRV, 51333 -d TestDB -U xxxx -P xxxx
I didn't find any issue that gave an answer to my problem.
odbcinst.ini
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-17.1.so.1.1
UsageCount=1
There's always seems to be an issue connecting to MS SQL Server using pyodbc from a linux machine. Is there a way to connect to SQL Server from Python. I'll appreciate your help in solving this error. Thank you.
[UPDATE]
As per the below answer, I updated the connection string. But, now I get following error:
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open
lib '/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1' : file
not found (0) (SQLDriverConnect)")
My odbcinst.ini file driver definition:
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1
UsageCount=1
It has always been a nightmare to connect to MS SQL Server from a Linux machine. Can you please tell which pyodbc, unixODBC and Driver version is the most stable?
I have installed the driver following this Microsoft instructions. My pyodbc version is 4.0.23
Microsoft's ODBC drivers for SQL Server do not use a PORT= parameter. The port number, if any, is appended to the server name/IP with a comma, e.g.,
SERVER=xxxTest-SRV,51333;
I ran into the same kind of issue, but my scenario is connecting to SQL server hosted on ec2 instance through AWS Lambda function using PyOdbc module. For me, replacing the host name with IP address of ec2 instance fixed it.
I found out that it was not able to resolve dns. So if any of the above steps didn't work for you, please try using the ip address and comment here
I ran into same problem when working with SQL Server docker container inside VS Code development container. Finding the IP of SQL Server container (credits for that goes to freecodecamp.org How to get docker container IP address) and replacing server name with IP solved the problem
conn = pyodbc.connect(
"DRIVER={ODBC Driver 17 for SQL Server};" + "SERVER=172.xx.x.x;"
"DATABASE=xxx;"
"UID=sa;"
"PWD=xxx;")
What solved in my case was to configure in the "SQL Configuration Manager" --> SQL Server Network Configuration --> Protocols for XYX --> TCP/IP --> Properties --> IP Addresses --> IPAII (Here set empty in TCP Dynamic Ports and 1433 (or the port that you want to use) at TCP Port)
You can have more details here
Microsoft's ODBC drivers for Linux cannot resolve instance names, so this won't work from a Linux client:
Server = mydbserver.mycompany.com\SQLEXPRESS
If you need to connect to a named instance you can use the (free) sqlserverport (https://github.com/gordthompson/sqlserverport) module.
import pyodbc
import sqlserverport
servername = 'myserver'
serverspec = '{0},{1}'.format(
servername,
sqlserverport.lookup(servername, 'SQLEXPRESS'))
conn = pyodbc.connect('DRIVER=ODBC Driver 17 for SQL Server;SERVER={};...'.format(serverspec))

Connecting to Azure SQL Data warehouse with active directory authentication using python from Ubuntu machine

I am trying to connect to azure sql database with active directory authentication using pyodbc on an ubuntu machine. I installed all the required packages on the server (pyodbc, unixodbc etc) and also installed sql server 17 odbc driver.
My server is ubuntu 18.04, so only odbc sql server 17 drivers are supported. The connection wont work with odbc 13 drivers.
I use the below connection details:
import pyodbc
server = 'xxxx.database.windows.net'
database = 'xxxx'
username = 'xxx#xxx.org'
password = 'xxxxx'
driver= '{ODBC Driver 17 for SQL Server}'
auth= 'ActiveDirectoryPassword'
conn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+',1433;DATABASE='+database+';UID='+username+';PWD='+ password+';Authentication=ActiveDirectoryPassword')
When I run the above connection details I face the below timeout error:
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
I even tried creating a connection string and using the below details but I still face the same issue.
pyodbc.connect(connection_string)
Am I missing any configurations? Please note that I tried this from a windows machine and was successful once I installed Microsoft Active Directory Authentication library for MS SQL Server and then by registering adalsql.dll.

Unable to connect to MS SQL Server using pyodbc on Scintific Linux 7.5

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.

Unix ODBC Connect 2016 MS SQL Server sql state 28000 native error 18456

I can connect ms sql server on windows desktop using ODBC and I got into the issue when using Linux. ODBC drivers are already installed just that I couldn't connect. I search on the internet and most posts are saying it's windows authentication issue. I set the Trusted_Connection=No and enter UID/PWD in isql -v MSSQL [UID] [PWD].
It gives me the error saying:
[28000][unixODBC][Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'UID'.
[ISQL]ERROR: Could not SQLConnect
The ODBC config file:
[root#IRISOEL01 etc]# cat odbcinst.ini
[ODBC]
Trace = Yes
TraceFile = /tmp/trace.log
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.1.0
UsageCount=3
[root#IRISOEL01 etc]# cat odbc.ini
[MSSQL]
Driver=ODBC Driver 13 for SQL Server
Server=server_address
Database=master
AnsiNPW=Yes
QuotedId=Yes
AutoTranslate=Yes
Trusted_Connection=No
Encrypt=No
I just can tell you to try using in odbc.ini the name of the driver you configured in odbcinst.ini
Driver=ODBC
After adding the driver path in odbcinst.ini, for example I use this odbcinst.ini
[SQLServer]
Description=Microsoft SQL Server ODBC Driver V1.0 for Linux
Driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0
Threading=1
UsageCount=1
then in odbc.ini
[source]
Driver=SQLServer
Description=ODBC Database Connection
Trace=No
Server=192.168.1.25,1433
Database=dbname

Resources