Perl script to connect oracle database - perl

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.

Related

How do I stop DBI connect from adding #localhost to my username?

This is a newbie question - don't overthink it!
I'm trying to make a connection from perl to a remote server's MySQL database that I can connect to just fine from PHP or from TOAD, so I know clearly what my host, dbname, username and password are.
But when I try to do this from my new installation of Strawberry Perl using the Padre IDE, something somewhere ( probably some config file ?) keeps insisting on adding '#localhost' to my username.
Oh, I'm using perl, v5.10.1 (*) built for MSWin32-x86-multi-thread.
I'm trying:
my $DBHOST = "50.88.64.41";
my $DBNAME = "keystrokes";
my $DBUSER = "wade\#'50.88.64.41'";
...
# MySQL database configuration
my $dsn = "DBI:mysql:database=$DBNAME,host=$DBHOST,port=3306";
So I get this output ( slightly sanitized for privacy):
DBI connect('database=keystrokes,host=50.88.64.41,port=3306','wade#50.88.64.41',...) failed: Access denied for user 'wade#50.88.64.41'#'localhost' (using password: YES) at C:\Bitnami\wampstack-8.0.8-0\apache2\htdocs\perl\firstscript.pl line 2.
Whether I put in my username as "wade" or as "wade#50.88.64.41" or "wade#'50.88.64.41'" SOMETHING then adds "#'localhost' to it, then tries to use that as the username, which of course fails, because I'm not on that machine and it is not my localhost.
Any ideas how to fix this? I couldn't find any explicit answers in this forum.
Thank you!
I think your problem is the exact opposite of what you think it is. MySQL is doing the right thing by adding '#localhost' to your username. What you don't need is the `#50.88.64.41' that you add. I've been connecting Perl programs to MySQL databases for fifteen years and I've never needed to add something like that to a username.
The '#host' that you'll see in MySQL connection messages is added by the MySQL client library when making the connection - by looking at the IP address that the connection request is coming from.
So just change your code to:
my $DBUSER = 'wade';
and it should work.
Traditionally, you'll see two rows for each user in the mysql.user table. One with a host of localhost and the other with a host of % (which means "any other host"). That makes it easier to control access to the database - it's common that you would only allow access to users who are on the same server as database.

How to change database from MySQL to Oracle in TheSchwartz

I'd like to switch database from MySQL to Oracle in perl's TheSchwartz module which is the job queue system. I suspects following code has the key.
my $client = TheSchwartz->new(
databases => [{
dsn => 'dbi:mysql:TheSchwartz',
user => 'dbi',
pass => 'xxxxxxx',
}],
verbose => 1,
);
I changed the code.
dsn => 'dbi:mysql:TheSchwartz',
to
dsn => 'dbi:Oracle:OraDB01',
then I got the message.
#Disabling DB 9e410d44ac4b9ede28c9ef34f6c1e817 because unknown
TheSchwartz doesn't tell me any clue of Oracle's error(ex password error ,or network error ....).
My question are
1. Is it possible to use Oracle in perl's TheSchwartz?
2. For dubugging ,how to get the ORA- Error message in TheSchwartz?
Any help would be welcomed.
Regards,
Using TheSchwartz is almost certainly a complete red herring here. TheSchwartz is just creating a DBI connection. So once you can successfully create a DBI connection outside of TheSchwartz, you should be able to do the same insider TheSchwartz.
The way I would approach it would be in two stages:
Write a simple DBI program to connect to the Oracle database.
Copy your successful connection details to your TheSchwartz code.
The simple DBI program I'd write, only needs to be as simple as this:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect('dbi:Oracle:OraDB01', 'dbi', 'xxx')
or die $DBI::errstr;
That will presumably fail in the same way as your existing code, but you'll get the raw DBI connection error which should allow you to work out what the problem is.
Obviously, you might find the DBD::Oracle documentation to be useful as well.

Connecting to a SQL Server DB

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.

Perl DBI connection to PostgreSQL doesn't die() on database error

I am used to Perl "crashing" my script when a SQL goes bad, but Postgres is just giving the error to the Apache error log and the script continues. Evil behaviour for debugging code.
Now I have asked the Oracle of Google but either I am asking the wrong questions or, very unlikely, I am the only one with that problem. ;)
In Short: I want the same fault behaviour with Postgres as MySQL does, ie, "crash" the script and take heed of use CGI::Carp qw(fatalsToBrowser); and just show me the faulty SQL statement.
I am using good old DBI to access the PG-DB, Postgres 8.4 and Perl 5.10.1 on Ubuntu 10.04 on my server, if that matters. All packages are from the official rep.
EDIT:
Thanks to Richard Huxton this is solved, leaving the solution for others to find.
our $dbh = DBI->connect($DBUrl, $DBUser, $DBPass,{ RaiseError => 1}
) || die "Could not connect to database: $DBI::errstr";
Surely you just need to set RaiseError when you make the connection?
https://metacpan.org/module/DBI

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