Since I'm programming a Perl script to monitor the SAP HANA database, I'm experiencing problems with the connection timeout. I don't know how to set the timeout. This is my connection string:
use DBI;
use DBD::ODBC;
$dbh = DBI->connect("dbi:ODBC:dsn=$dsn");
Meanwhile in another script to monitor a Sybase database it works properly:
use DBI;
dbh = DBI->connect("dbi:Sybase:server=$dbsvr;database=$dbname;".
timeout=$timeout;"."loginTimeout=$timeout", $user, $pass);
I think the problem comes with the ODBC. Which is the way to implement a timeout using ODBC connection?
I don't believe there is a way to set SQL_ATTR_LOGIN_TIMEOUT on the connection handle via DBD::ODBC. In any case, few ODBC drivers do anything with it. However, if you are on UNIX and using the unixODBC driver manager - see Setting ODBC driver environment variables automatically
You basically add "DMConnAttr = CONNECTION_ATTRIBUTE=value" to you odbc.ini file where CONNECTION_ATTRIBUTE is an ODBC connection attribute (e.g., SQL_ATTR_CONNECTION_TIMEOUT) and value is what you want to set it to.
It is commom to confused with connection and query timeout:
DMStmtAttr = SQL_ATTR_QUERY_TIMEOUT=2
DMConnAttr = SQL_ATTR_CONNECTION_TIMEOUT=2
Connection may be ok but query response take more time than normal
Related
I'm attempting to connect to a Postgres database that requires the client pass the paths to client and server SSL certificates using the ODBC driver for postgres. I'm using psqlodbc v11 x86 on Windows 10. I need to have three options passed to the ODBC driver sslrootcert, sslcert, and sslkey. I know that the paths are not being passed by the odbc driver since when I connect using the following connection string where I specify the pqopts it is able to connect.
Driver={PostgreSQL UNICODE};Server=XXXX;Port=5432;Database=XXXX;Uid=XXXX;Pwd=XXX;sslmode=verify-ca;pqopt={sslrootcert=C:\\ssl\\pgSQL.ca.cert sslcert=C:\\ssl\\pgSQL.cert sslkey=C:\\ssl\\pgSQL.key}
According to the document section
Advanced Options 3/3 Dialog Box->Libpq parameters, I should be able to pass the parameters by typing the values within the braces directly into the text box. It displays the error message saying that it cannot find the certificate because it is looking in the default location and is not using the value that was provided in the Libpq text box. Am I doing something wrong? Any advise on how to connect passing client certificates would be greatly appreciated.
Answering this in case anyone else finds this useful. The problem seems to be that the Test button on the ODBC Driver doesn't take the options set in the libpq parameters into account and just uses the default settings. When I saved the ODBC connection closed the ODBC Data Source Administrator window and used the connection from a different program it was able to connect successfully.
I want to add a new Informix sever entry into sqlhosts, but I'm not quite sure how it will impact the existing connection.
Currently sqlhosts contains only one server entry...
dbserver onsoctcp 111.111.111.20 7101
The database handle is created within an existing perl module (db is a database on the server)...
my $dsn = "DBI:Informix:db";
my $dbh = DBI->connect($dsn,"user","password");
Notice that "dbserver" is never referenced.
I want to add a test server to sqlhosts. Something like this...
dbserver onsoctcp 111.111.111.20 7101
dbserver_test onsoctcp 111.111.111.21 7101
With only one entry in sqlhosts, everything has been working fine. But my connection never references the server name in sqlhosts.
So, my question(s)...
Does Informix just try to use the only one available?
Will adding a second server entry in sqlhosts force me to include the server name in the connection string?
Thanks!
Informix client uses environment variables to resolve hosts and other configuration; check that INFORMIXDIR is set to the path where Informix CSDK is installed (I assume it is), and set INFORMIXSERVER to point to the new entry in sqlhosts. See this article in IBM knowledge base.
Alternatively, use db#server data source format:
my $dbh = DBI->connect("DBI:Informix:db#server", "user", "password");
Maybe it is a permissions issue? From the documentation:
Note that you might also be able to connect to other databases not
listed by DBI->data_sources using other notations to identify the
database. For example, you can connect to "dbase#server" if "server"
appears in the sqlhosts file and the database "dbase" exists on the
server and the server is up and you have permission to use both the
server and the database on the server and so on. Also, you might not
be able to connect to every one of the databases listed if you have
not been given at least connect permission on the database. However,
the databases listed by the DBI->data_sources method certainly exist,
and it is legitimate to try connecting to those sources.
http://search.cpan.org/~johnl/DBD-Informix-2013.0521/Informix.pm
I’ve been trying to connect to a SQL Server DB in Perl since yesterday, and it’s a failure. I use the DBI->connect function, but I got error saying the data source and the driver are not found. I use that string:
'dbi:ODBC:mydb:myhost\myinstance'
I’m not really sure the string’s correct. Any idea?
EDIT: here’s the error I got:
DBI connect('***:***\***','',...) failed: [Microsoft][ODBC Driver
Manager] Data source name not found and no default driver specified (SQL-IM002)
at Collector.pl line 137
The format of the DBI connect string for ODBC is 'dbi:ODBC:DSN_name' or 'dbi:ODBC:DSN=DSN_name' (for connections using Data Source Names (DSNs). In that case DSN_name needs to be the name of a data source you created with the ODBC administrator. This situation is slightly more complicated in Windows 64 bit environments as there are 2 ODBC administrators, one for 32 bit applications and one for 64 bit applications (you cannot mix the two).
So, first, work out if you Perl is 32 bit or 64 bit executable - you can usually see this from the output of perl -V (look for archname but there are other indications too). Then find the right ODBC Administrator depending on how your perl was built and create the data source with it. Lastly, change your DBI connect string as above.
There are also so called DSN-less connections that do not require a DSN but instead you name the DRIVER and any attributes required to connect to the database.
There differences in what DBD::ODBC does if you use 'dbi:ODBC:DSN_name' instead of 'dbi:ODBC:DSN=DSN_name' which you can read about ODBC - The Connection Process.
You can find out more about 32/64 bit ODBC and where to find the right driver manager at 64 bit ODBC. You can find out about DBD::ODBC and connections at Drivers, Data Sources and Connection - Perl DBI/DBD::ODBC Tutorial Part 1.
Well this seems to work ok,
use DBI;
#
$dbh = DBI->connect( "dbi:mysql:DBNAME", "root", "password" )
or die "Connection Error $DBI::errstr\n";
N.B. Please Check you have the required modules installed.
I currently have a Macro which is connecting to a sybase database using ODBC. It's making the connection by running the following:
Set conX = wrkODBC.OpenConnection("Connection1", , True, strConn)
where strConn = "ODBC;DSN=Server_Name;APP=Daily Task;DB=db_name;UID=uname;PWD=pwd;"
The problem I am having is that this connection is working for some Server_Name's but not others. When it doesn't work I get a Error "3146 - ODBC--call failed".
What I don't understand is where it is pulling the server details from. So for example, when using embarcadero rapid to connect to a sybase database, it will use the sql.ini file to pull the server connection details based on the server_name.
I have checked and all the Sybase Servers I am testing are in the sql.ini file and my env variables are pointing to the correct sybase version. I've checked ODBC and it is only picking up a SQL Server driver.
Can anyone please explain how ODBC pulls the server name connection details? I don't understand why it works for some server names, but not others (FYI, I have tested the uname and pwd is rapid to make sure it is correct).
Any information would be much appreciated.
So, the issue was that I was looking at my odbc connections under control panel. I should mention that I am on windows 7. What I had to do was update my ODBC connection details here:
C:/Windows/SYSWOW64/odbcad32.exe
--This is for 32bit!
This had some connections set up which is why I could connect to some servers but not all of them.
I have been asked to look into using ODBC as a database driver to be able to use SQL on an Excel workbook. I have seen a number of people using OLE as a driver, but the only ODBC walkthroughs I've seen are regarding connecting to an MSSQL Server or MySQL.
I have confirmed that i have ODBC setup and that it below are the ODBC drivers i have available. Can anyone shed some light on connecting to an XLS file?
Available Drivers:
DBI Drivers:
Chart, DBM, ExampleP, File, ODBC, Oracle, Proxy, SQLite, SQLite2, Sponge, mysql
ODBC Drivers:
DBI:ODBC:MS Access Database
DBI:ODBC:Excel Files
DBI:ODBC:dBASE Files
DBI:ODBC:Visio Database Samples
DBI:ODBC:Xtreme Sample Database 2003
DBI:ODBC:Xtreme Sample Database 2008
inside test.pl
my $dbh = DBI->connect('DBI:ODBC:Driver{Excel Files}MyExcelFile');
I'm not in front of a Windows machine right now but this is approximately what you need to do. Find the ODBC Administrator and depending on whether you are going to be the only one running your Perl or others as well create a USER or SYSTEM DSN. Select Excel as the driver from the list and click add then fill in any required fields you are asked for - at least the location of the excel file. Give the DSN a name.
Now use DBI->connect('dbi:ODBC:DSN=name_you_gave_DSN');
Once connected, read about odbc_out_connect_string attribute which returns the ODBC out connection string. It will look something like:
Driver={Excel Files};workbook=c:\x.xls;something=somethingelse;
You can use that string instead of DSN=name_you_have_DSN in the connect call now and you'll no longer need the DSN you created - so called DSN-less connection.
After that there are loads of tutorials on using DBD::ODBC including the ones at http://www.easysoft.com/developer/languages/perl/index.html
I use the Microsoft Excel Driver outlined here in what's called a "DSN-less connection" where only the driver is specified and you specify the Excel file as a parameter in the connection string itself: http://www.connectionstrings.com/excel-2007-odbc/
my $file = 'c:\temp\myfile.xslx';
my $dbh = DBI->connect('dbi:ODBC:driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ='.$file.';');
my $sth = $dbh->prepare( "SELECT * FROM [Sheet1\$]" );
$sth->execute();
while (my $row = $sth->fetchrow_hashref) {
print Dumper( \$row );
}
Watch out if you are running 64 bit perl and need to access a 32 bit ODBC driver though. You can't mix bits like that and have to resort to using 32 bit perl or some kind of ODBC bridge: Can i use a 32 Bit ODBC Driver for my 64 Bit app
More recently I have had to install the Microsoft Access Redistributable to get the Excel driver on Windows 10: https://stackoverflow.com/a/54757113/74585
To connect you need connect string. There you can use DSN version ad bihica described, or show what driver you can use and use driver specific properties. For Excel this can look like:
Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=C:\MyExcel.xls;DefaultDir=c:\mypath;
You will find more examples at: http://www.connectionstrings.com/excel#p86