Installation of Oracle client to Ubuntu 10.04 Lucid

Recently I moved from Fedora 13 to Ubuntu 10.04 on my work laptop. So, I faced again with problem to setup Oracle PHP interface. Oracle provides RPMs only and not DEBs. So, to install the client RPMs should be converted to the DEBs. It can be done easily using utility alien:
sudo alien oracle-instantclient11.1-basic-11.1.0.7.0-1.i386.rpm
sudo alien oracle-instantclient11.1-devel-11.1.0.7.0-1.i386.rpm
sudo alien oracle-instantclient11.1-sqlplus-11.1.0.7.0-1.i386.rpm

They can be installed then with sudo dpkg -i.
Don’t forget to install PHP interface to Oracle:
sudo pear install pecl/oci8
That’s it. I spent no more 15 minutes to setup PHP Oracle interface under Ubuntu. Thanks to that post!

Add history and auto-complete to the SQL*Plus

I was very surprised how pure the Oracle’s command line utility SQL*Plus. Comparing with mysql it lack history, completion and doesn’t allow even edit query. To fix that the utility rlwrap can be used. I installed it via yum under Fedora 11 and run like that:
rlwrap sqlplus db_user@db
To have auto-complete feature you should find file SQL.dict somewhere (drop the comment, please, if you know the place). It contains SQL keywords, functions and commands. The command to run SQL*Plus will be following in that case:
rlwrap -b "" -f $HOME/sql.dict sqlplus db_user@db

To make our life easier we can create an alias in the .bash_profile or .profile:

alias mysqlplus='rlwrap -b "" -f $HOME/sql.dict sqlplus'

There is an another interesting improvement for Linux users – using VIM as default editor of SQL*Plus. I didn’t try it practically yet.

[via Oracle Online]

Oracle date format and Perl

Oracle has its own date format – DD-MM-YY (24-sep-09). So, to insert a value into Oracle date field you should somehow convert date from your format. Sure it can be done using some date/time module or your own function. But there are two simpler ways – set desire date/time format for session or use Oracle date conversion function.

To set specific date/time format for the session just run following query right after connection:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'
After that you may insert date such 2009-09-24 without any problems.

Another way is convert date to the Oracle format inside the query:
INSERT into some_table (d_start) VALUES (TO_DATE('2009-09-24 11:12:00', 'YYYY-MM-DD HH24:MI:SS'))

Both approaches work fine. But the first one is more appropriate if you use placeholders in your query.

Connect to Oracle DB from Perl script

After successful installation of DBD::Oracle it’s time to use it. The connection string is the same as for he rest DB:
my $dbi = DBI->connect("dbi:Oracle:$db_name:$db_host:$db_port", $db_user, $db_pass);
As result of running code above I got following error:
Couldn't connect to database db_name: ORA-12154: TNS:could not resolve the connect identifier specified (DBD ERROR: OCIServerAttach)
After googling I found that the problem was that. I tried to connect to the remove database but the driver couldn’t do that without special file – tnsnames.ora. It should be placed to the $ORACLE_HOME/network/admin and contain something like that:
db_name =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = db_host)(PORT = db_port))
)
(CONNECT_DATA =
(SERVICE_NAME = db_name)
)
)

And the connection string should be changed to use service name from the tnsnames.ora instead of host:
my $dbi = DBI->connect("dbi:Oracle:$service_name", $db_user, $db_pass);
Finally we should export variable ORACLE_SID into our environment. Add this command into .bashrc
export ORACLE_SID="orcl"
or set it using Perl variable $ENV:
$ENV{ORACLE_SID} = 'orcl';

See also

Installing DBD::Oracle under Fedora 11

Recently I got a task which needs to communicate with database Oracle from Perl. Perl has an excellent database abstraction interface DBI. But for specific database it needs a driver – DBD module. To install DBD::Oracle you have to perform following simple steps:

  1. Download and install oracle-instantclient11.2-basic and oracle-instantclient11.2-devel RPMs from Instant Client Downloads for Linux x86 page.
  2. Export ORACLE_HOME:
    export ORACLE_HOME=/usr/lib/oracle/11.2/client/
  3. Install DBD::Oracle using cpan shell or manually.

The most difficult part for me was installing Oracle client. Because navigation on the Oracle web site is not clear. Additionally you can install SQL*Plus package (oracle-instantclient11.2-sqlplus) to use Oracle shell for testing.

The RedHat Enterprise and CentOS users can use Perl-DBD-Oracle RPM to avoid separate Oracle client installation.