Connecting to a SQL Server DB - perl

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.

Related

ODBC to SAP HANA connection timeout in Perl

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

Perl script to connect oracle database

I am new to Perl Programming and database connectivity. Can anyone please let me know step by step procedure to write Perl Script for Oracle Database connectivity.
My Perl Verion is:
This is perl 5, version 22, subversion 0 (v5.22.0) built for MSWin32-x64-multi-thread
Copyright 1987-2015, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
I have tried the below:
my $db = DBI->connect("dbi:Oracle:Local","SYSTEM","SYSTEM") or die print ("could not connect! $DBI::errstr \n");
Since I don't know what is this "dbi:Oracle:Local" I could not able to connect to Database.
Can you please let me know what is dbi, Oracle, local. if it is Hostname and oracle database name, how can I find the same in my computer.
Do I need to set any ENV variable in Perl? If so, where and what I need to set?
dbi:Oracle lets DBI know which driver to use. If you're connecting to an Oracle database, you'll never change these.
Local is either an actual database name on the local system, or a name listed in TNSNAMES.ORA. Substitute the name of the local database you'd like to connect to.
The next two parameters are username and password.
If you're connecting remotely, or need to do something more elaborate, consult the docs, or one of the many available guides.
my $db = DBI->connect("dbi:Oracle:Local","SYSTEM","SYSTEM");
"dbi" is a string,
"Oracle" is the driver type,
"local" is the database name,
First "SYSTEM" is the username and second one is password.
use DBI;
my $dbh = DBI->connect( "dbi:Oracle:databaseName", 'username', 'passwd' ) or die($DBI::errstr, "\n");
First read the basic concept of Simple Database access using Perl DBI and SQL.

SQL linked server with filemaker

I have a client with FileMaker Pro 11 Advanced.
I need to be able to connect to the filemaker database via SQL. I am using MS SQL Server 2008 R2, or Server 2012 can be used.
These are the steps I have tried
0) Turned on ODBC/JDBC sharing in File -> Sharing on FileMaker Pro 11 Advanced for "All Users"
1) installed the FileMaker OBDC driver included on the filemaker disc
2) odbcad32.exe in C:\Windows\SysWOW64 and added a new filemaker System DSN and directed it to the filemaker database.
3) Attempted to add a linked server to SQL using Provider Microsoft OLE DB Provider for OBDC Drivers and the System DSN name i created for Data Source. I left all other fields blank.
I get this error:
Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "FILEMAKER2".
OLE DB provider "MSDASQL" for linked server "FILEMAKER2" returned message "[Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application". (Microsoft SQL Server, Error: 7303)
FILEMAKER2 is the what i called the System DSN.
What am I doing wrong/What do I need to correct in order to connect to filemaker with SQL?
Is there a connection string to put in for Provider String?
I have the same mismatch error trying to do a test connect with access.
Thank you for any help.
The error you're getting is because of the driver installed is a 32bit driver propably on a 64bit system.
Look in the downloads of FM for the correct xbit driver.
I've connected to FM using ODBC from PHP apps to do basic inserts, updates, deletes of data. I haven't worked with it like you're attempting to do, though, and like #ted said, FM really wasn't built as a SQL platform.
Their answer to this was to go the other way around. You can setup External SQL sources within FileMaker, and bind your SQL database to FM so that those tables show up in FM the same as regular FM tables. These tables can then be used within FM scripts accordingly, so you could setup any data sync steps necessary there. You could create scripts to move data from FM layouts into the SQL DB layouts at given intervals throughout the day, for example.
Hope that helps.
FileMaker 11 only comes with a 32 bit ODBC driver. The error you got happens when you use a 32 bit driver with the 64 bit ODBC admin utility. Set up your DSN using the 32 bit utility, found here:
C:\Windows\SysWOW64\odbcad32.exe
...and it will work fine.

connect failed with SQL-HY001

I am using Perl 5.14.2 on Ubuntu wheezy 64 bit.
I am trying to connect to a Pervasive SQL server v9.5 that is installed on a windows 2008 machine.
I tested the connection with isql and it works properly, I tested with the following command:
isql -v <db_name>
I use the DBD:ODBC v1.39 (latest version) perl module installed from cpan.
I am using the following command to connect to sql server:
use DBI;
my $db = DBI->connect('dbi:ODBC:Moked');
Moked is the connection name that was defined in the unixodbc v2.2.14 in odbc.ini.
/etc/odbc.ini
[Moked]
Driver=PERVASIVE_ODBC
Description=Pervasive ODBC Interface: Moked
ServerName=<ip>:<port>
DBQ=MIDA
UID=
PWD=
OpenMode=0
PvTranslate=
when I try to connect with the command i showed above I get error SQL-HY001 that by googling I understood that it relates to memory allocation, usually people get these while querying and not while trying to connect/
it seems that DBI does recognize the connection because if I type a different name, for example 'Moked2', i get the error SQL-IM002 instead.
any ideas?
how can I debug this issue further?
any information regarding the issue would be greatly appreciated.
thanks!
You are getting the memory allocation error because the PSQL v11 client cannot reliably connect to a PSQL v9.5 server. If you want to use the v11 client, you need the v11 server too. You might be able to get away with the v11 client at a Btrieve level but there were significant changes between v9 and v11 on the ODBC side.

How do i connect Perl with an Excel file (*.xlsx or *.xls) using ODBC?

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