Pyodbc error in Python How to fix connection - sql-server

I am trying to Connect to sql server database but I get this error:
File "C:\Python37-32\SqlVersionpr_import.py", line 528, in proceed
with pyodbc.connect('DRIVER={ODBC Driver 17 for SQL
Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password) as myDbConn:
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found
and no default driver specified (0) (SQLDriverConnect)')
My code:
server = '***********'
database = '*****'
username = '***********'
password = '***********'
with pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password) as myDbConn:
with myDbConn.cursor(as_dict=True) as cursor:
cursor.execute(''' select * from empInfo where ssn = %s and storeid = %s ;''',(strLastSsn,stono))
emp_rows = cursor.fetchall() or []

Related

Driver's SQLAllocHandle on SQL_HANDLE_HENV failed while connecting extremeDB ODBC driver

Getting the following error while running
isql EXDB -v
[IM004][unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed
[ISQL]ERROR: Could not SQLConnect
Below are contents of odbc.ini
[EXDB]
Description = eXtremeSQL data source
Driver = eXtremeSQL
#DATABASE = exdb
SERVER = localhost:5099
odbcinst.ini :
[eXtremeSQL]
Description = ODBC for eXtremeSQL
Driver =
Setup =
Driver64 = /root/extremeDB-v8.0.1799.25104.1/odbc/bin.so/libmcoodbc_drv_debug.so
Setup64 =
FileUsage = 1
isql command is giving some error. Please help

[08001][Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x2746 - Ubuntu 22.04

I'm trying to connect to an MS SQL Server 2016 from my ubuntu server 22.04.
I've installed the odbc driver following the official MS documentation: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16
The installation works fine and I can connect to newer MS SQL servers, the problem is to connect to 2016 SQL Server, looks like an issue with OpenSSL version. Ubuntu 22.04 uses by default the openssl 3.x.x.
I've enabled some debug options by adding to /etc/odbcinst.ini
[ODBC]
Trace = Yes
TraceFile = /dev/stdout
And the output doesn't help much. I've found some solutions but are always related to old ubuntu.
Here is the output:
[ODBC][62430][1664301799.879691][__handles.c][460]
Exit:[SQL_SUCCESS]
Environment = 0x55b6b35d3870
[ODBC][62430][1664301799.879839][SQLAllocHandle.c][377]
Entry:
Handle Type = 2
Input Handle = 0x55b6b35d3870
UNICODE Using encoding ASCII 'UTF-8' and UNICODE 'UCS-2LE'
[ODBC][62430][1664301799.880405][SQLAllocHandle.c][513]
Exit:[SQL_SUCCESS]
Output Handle = 0x55b6b35d4590
[ODBC][62430][1664301799.880465][SQLDriverConnect.c][748]
Entry:
Connection = 0x55b6b35d4590
Window Hdl = (nil)
Str In = [Driver={ODBC Driver 18 for SQL Server};Server=tcp:xxxx,1433;Database=xxx;UID=xxx;PWD=**********;][length = 116 (SQL_NTS)]
Str Out = 0x7fffe858da30
Str Out Max = 2048
Str Out Ptr = (nil)
Completion = 0
[ODBC][62430][1664301799.903128][__handles.c][460]
Exit:[SQL_SUCCESS]
Environment = 0x55b6b370ad10
[ODBC][62430][1664301799.903245][SQLGetEnvAttr.c][157]
Entry:
Environment = 0x55b6b370ad10
Attribute = 65002
Value = 0x7fffe8586090
Buffer Len = 128
StrLen = 0x7fffe858602c
[ODBC][62430][1664301799.903298][SQLGetEnvAttr.c][273]
Exit:[SQL_SUCCESS]
[ODBC][62430][1664301799.903319][SQLFreeHandle.c][220]
Entry:
Handle Type = 1
Input Handle = 0x55b6b370ad10
DIAG [08001] [Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x2746
DIAG [08001] [Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection
[ODBC][62430][1664301799.903893][SQLDriverConnect.c][1637]
Exit:[SQL_ERROR]
[ODBC][62430][1664301799.903976][SQLError.c][424]
Entry:
Connection = 0x55b6b35d4590
SQLState = 0x7fffe858f046
Native = 0x7fffe858f040
Message Text = 0x7fffe858f050
Buffer Length = 500
Text Len Ptr = 0x7fffe858f03e
[08001][Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x2746
[08001][Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection
Also, I've installed the OpenSSL 1.0.1k manually compiling the code, but looking the strace from the isql or sqlcmd it stills using the openssl 3 has you can see here:
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libcrypto.so.3", O_RDONLY|O_CLOEXEC) = 4
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libssl.so.3", O_RDONLY|O_CLOEXEC) = 4
Do you know how to change the libssl from the odbc drive?

SQL IM002 Errors while connecting to SQL Server from perl on heroku

I have a perl application hosted on heroku which needs to connect with some sql server. I am unable to establish connection. It fails with following error:
DBI connect('Driver={ODBC Driver 11 For SQL Server}; Server=****; UID=****; PWD=****','',...) failed: [unixODBC][Driver Manager]Data source name not found, and no default driver specified (SQL-IM002) at work.pl line 12.
This is the code.
work.pl
use strict;
use warnings;
use DBI;
my $DRIVER = '{ODBC Driver 11 for SQL Server}';
my $SERVER = '****';
my $UID = '****';
my $PWD = '****';
my $x = DBI->connect("dbi:ODBC:Driver=$DRIVER; Server=$SERVER; UID=$UID; PWD=$PWD");
Relevant Environment Vars:
LIBRARY_PATH=/app/.platform/vendor/usr/lib64:
LD_LIBRARY_PATH=/app/.platform/vendor/usr/lib64:
PATH=/app/.platform/vendor/usr/bin:/app/vendor/perl/bin:/usr/bin:/bin
LANG=en_US.UTF-8
ODBCSYSINI=/app/.platform/vendor/etc
HOME=/app
PWD=/app
ODBCINI=/app/.platform/vendor/etc/odbc.ini
ODBCHOME=/app/.platform/vendor/etc/
PERL5OPT=-Mlocal::lib=/app/vendor/perl-deps
odbcinst.ini:
Description = ODBC for PostgreSQL
Driver = /usr/lib/psqlodbc.so
Setup = /usr/lib/libodbcpsqlS.so
Driver64 = /usr/lib64/psqlodbc.so
Setup64 = /usr/lib64/libodbcpsqlS.so
FileUsage = 1
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc5.so
Setup = /usr/lib/libodbcmyS.so
Driver64 = /usr/lib64/libmyodbc5.so
Setup64 = /usr/lib64/libodbcmyS.so
FileUsage = 1
[ODBC Driver 11 for SQL Server]
Description = Microsoft ODBC Driver 11 for SQL Server
Driver = .platform/vendor/opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0.so.2270.0
Threading = 1
UsageCount = 1
odbc.ini is empty.
Am I missing something?
Tried everything and found the reason. It should be dbd:ODBC:DRIVER instead of dbd:ODBC:Driver.

R connection to MS SQL

Using R I am trying to connect to MS SQL 2014 on an Azure VM (not windows authentication)
library(RODBC)
conn <- odbcDriverConnect(connection = "Driver=SQL Server;Server=someinternetmachine.cloudapp.net;Database=MyDatabase;Uid=MyUsername;Pwd=MyPassword;")
queryResult <- sqlQuery(conn, "SELECT top 10 * FROM sometable")
With RODBC is there anywhere to do this using just a connection string (no DSN)?
Using the above I get the below errors
Warning messages:
1: In odbcDriverConnect("driver={SQL Server};server=servername\\instancename,port;database=testing;uid=abc;pwd=123456") :
[RODBC] ERROR: state 08001, code 6, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]Specified SQL server not found.
2: In odbcDriverConnect("driver={SQL Server};server=servername\\instancename,port;database=testing;uid=abc;pwd=123456") :
[RODBC] ERROR: state 01000, code 11001, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()).
3: In odbcDriverConnect("driver={SQL Server};server=servername\\instancename,port;database=testing;uid=abc;pwd=123456") :
ODBC connection failed
Did you choose public provisioning for the VM in Azure? Additionally, you need to open the ports for SQL Server in Windows Firewall (port number depends on default or named instance). Also, in case of named instance if you are using dynamic ports then SQL Browser also needs to be opened up. More information can be found in the link here.
Managed to get this working with
driver.name <- "SQL Server"
db.name <- "master"
host.name <- "someinternetmachine.cloudapp.net"
port <- ""
server.name <- "someinternetmachine.cloudapp.net"
user.name <- "MyUsername"
pwd <- "MyPassword"
# Use a full connection string to connect
con.text <- paste("DRIVER=", driver.name,
";Database=", db.name,
";Server=", server.name,
";Port=", port,
";PROTOCOL=TCPIP",
";UID=", user.name,
";PWD=", pwd, sep = "")
con1 <- odbcDriverConnect(con.text)
res <- sqlQuery(con1, 'select * from information_schema.tables')
odbcCloseAll()

SQL Server ODBC connection failed

I am trying to connect SQL server using ODBC.
Could some one help interpreting what this error is and how this can be rectified?
Kindly note that there are no password issues as I use the same credentials to connect to the SQL server using Aqua studio.
dbhandle <- odbcDriverConnect('driver={SQL Server};server=SQLBBAQA;database=bbadb;uid = "aaa_bbb_ccc", pwd = "aaabbbccc123&" ')
Warning messages:
1: In odbcDriverConnect("driver={SQL Server};server=SQLBBAQA;database=bbadb;uid = \"aaa_bbb_ccc\", pwd = \"aaabbbccc123&\" ") :
[RODBC] ERROR: state 08001, code 17, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied.
2: In odbcDriverConnect("driver={SQL Server};server=SQLBBAQA;database=bbadb;uid = \"aaa_bbb_ccc\", pwd = \"aaabbbccc123&\" ") :
[RODBC] ERROR: state 01000, code 2, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()).
3: In odbcDriverConnect("driver={SQL Server};server=SQLBBAQA;database=bbadb;uid = \"aaa_bbb_ccc\", pwd = \"aaabbbccc123&\" ") :
[RODBC] ERROR: state 01S00, code 0, message [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
4: In odbcDriverConnect("driver={SQL Server};server=SQLBBAQA;database=bbadb;uid = \"aaa_bbb_ccc\", pwd = \"aaabbbccc123&\" ") :
ODBC connection failed
I see multiple mistakes in the connection string:
server=SQLRAPQA should be in the form server=MACHINE\INSTANCE. Use server=.\SQLRAPQA if the instance is located on the same machine.
Remove all whitespaces.
Use ; as separator, not ,.
As referenced by zx8754, in RODBC odbcDriverConnect() Connection Error it is shown that a connection string should look like:
'driver={SQL Server};server=servername\\instancename,port;database=testing;uid=abc;pwd=123456' . Note the double occurences of \, this seems to be rodbc specific.

Resources