Accessing an Oracle database from Perl

Posted on the February 23rd, 2009 under Computers,Linux by

In the previous post I described how to install the Oracle Database 10g Express Edition on Ubuntu and to add some data. In this article I will show how to access that data from a Perl script.

I assume you already have Perl installed, together with the DBI framework for generic database support. We will need to add DBD::Oracle which is available from CPAN. To install this module, run this as root:

# perl -MCPAN -e shell
cpan> install DBD::Oracle

Two things are worth noting. First, the root user must have the same Oracle environment set as described in the previous article. Just issue ‘source /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh’ to fix that. The second thing to note is that you will most likely get errors during testing of the newly built module which will prevent it from being installed. To override that you will need to add ‘force’ before the command, i.e.:

cpan> force install DBD::Oracle

Now, the groundwork is done and we just need to write the script. Save this in oracle_read.pl and make it executable.

#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect( 'dbi:Oracle:xe',
      'scott',
      'tiger',
      ) || die "Database connection not made: $DBI::errstr";

my $sql = qq{ SELECT id,name,age FROM persons };
my $sth = $dbh->prepare($sql);
$sth->execute();
my($id, $name, $age);
$sth->bind_columns(\$id, \$name, \$age);

print "List of persons:\n";
while( $sth->fetch() ) {
    print "$name [$age]\n";
}
$sth->finish();
$dbh->disconnect;
Share

Oracle under Ubuntu

Posted on the February 23rd, 2009 under Computers,Linux by

I am a rather proficient user of MySQL but I recently needed to set up an Oracle database to test against. Since I haven’t worked with Oracle databases in more than ten years it took literally hours to get something up and running. What follows is a description on how to install Oracle XE on Ubuntu 8.10, add a user, create a table and then drop the created schema. Everything will be done from the command line.

Note that this installs the Oracle Database 10g Express Edition. Although free it comes with similar constraints as the Microsoft SQL Server 2008 Express server – maximum 4 GB data, use maximum 1 GB of RAM and run on maximum 1 CPU.

First, add the Oracle repositories by appending the following two lines to /etc/apt/sources.lst

# Oracle Repository

deb http://oss.oracle.com/debian unstable main non-free

Then, we must add the Oracle key to avoid warnings:

wget http://oss.oracle.com/el4/RPM-GPG-KEY-oracle -O- | sudo apt-key add -

Now we are ready to install by using apt-get (either run the following as root or prepend every line with sudo)

# apt-get update
# apt-get install oracle-xe oracle-xe-client
# /etc/init.d/oracle-xe configure

The last command will present options to change some default settings. you can probably use most of the defaults. I just changed the HTTP port from 8080 since I already had another service running on that port. Note that the configuration script takes a very long time to finish.

When the script is finished it is possible to access the Oracle web interface at http://127.0.0.1:8080/apex (or some other port if you changed the default). However, it will only be accessible on the local host. If you are installing on a headless remote server like me you can port-forward using SSH:

$ ssh -L 8080:127.0.0.1:8080 user@host

Or, you can remove this limitation as described further below.

Next step will be to add the Oracle environment to your shell. The installation will have added scripts to set up the environment under /usr/lib/oracle/xe/app/oracle/product/10.2.0/. There are actually two scripts: server/bin/oracle_env.sh and client/bin/oracle_env.sh. I don’t think it matters which one but it makes more sense to use the one under ‘server’.

Append this to your .bash_profile file:

. /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh

Then log out and back in – or run ‘source .bash_profile’.

Now it is time to log onto the system:

$ sqlplus system@localhost

If you would have logged on remotely you would have written ‘sqlplus system@host’ – but we haven’t enabled remote access yet.

Type the password you selected during installation. At the SQL prompt, enter:

> EXEC DBMS_XDB.SETLISTENERLOCALACCESS(FALSE);
> EXIT;

Now it is time to add the first user. Save the following to a file (oracle_create.sql).

-- oracle_create.sql
CREATE USER scott IDENTIFIED BY tiger
    DEFAULT TABLESPACE users TEMPORARY TABLESPACE temp
QUOTA UNLIMITED ON users;
CREATE ROLE myrole;
GRANT CREATE session, CREATE table, CREATE view,
    CREATE procedure, CREATE synonym TO myrole;
GRANT myrole TO scott;
-- Switch user
CONNECT scott@localhost/tiger;
--
CREATE TABLE persons (
	id int,
	name varchar2(32),
	age number
);
--
INSERT INTO persons
	(id, name, age)
	VALUES (1, 'Joe', 35);
INSERT INTO persons
	(id, name, age)
	VALUES (2, 'Mary', 32);

You can then create the user and populate the table by running:

$ sqlplus system@localhost
> @oracle_create
> SELECT * FROM persons;
> EXIT

The ampersand indicates that SQL statements should be read from a file. Since the file ends with .sql the extension does not have to be stated. Please note the connect statement in the SQL file. This means that after that point we will be running as the user scott.

To drop everything we have created run the following:

$ sqlplus system@localhost
> drop user scott cascade;
> drop role myrole;
Share

CSS-based rounded corners

Posted on the February 22nd, 2009 under Computers,Web design by

Having rounded corners in web layout is usually a nice touch, whether it be menu tabs, boxes or whatever. Way back when the layout was done using HTML tables this could be achieved with images and a bunch of extra table cells. Ugliest solution there ever was but it worked. The last few years I have been using Javascript libraries such as NiftyCube to achieve more or less the same effect.

In CSS3 there are finally options for creating rounded corners. It is worth noting that CSS3 is still under development and that this technique only works under Firefox and Safari – but since they are the browsers I use anyway it is almost good enough for me already.

Users of Firefox and Safari should see this text in a blueish box with rounded corners. Users of other browsers will see a box with square corners.



The HTML code for the box above is

<div style="width:300px;
background-color: #ddddff;
-moz-border-radius: 15px;
-webkit-border-radius: 15px;
border: 4px solid #8888ff;
padding: 10px;" >

The added options are (for the two browser families):

-moz-border-radius: 5px;
-webkit-border-radius: 5px;

It is also possible to round only certain corners by using a combination of the following:

-moz-border-radius-topleft
-moz-border-radius-topright
-moz-border-radius-bottomleft
-moz-border-radius-bottomright
-webkit-border-radius-topleft
-webkit-border-radius-topright
-webkit-border-radius-bottomleft
-webkit-border-radius-bottomright

Share

Configure postfix to use smart host

Posted on the February 22nd, 2009 under Computers,Linux by

My ISP filters outgoing SMTP traffic so to be able to run a mail server running at home I need to use the ISPs SMTP server to relay outgoing email. If they would have allowed open relaying from their customers I could just have set their SMTP server as ‘relayhost’ in /etc/postfix/main.cf and be done with it.

However, they require that I authenticate to their SMTP server which complicates matter slightly. This is how I did it:

  1. Create a password file, assigning username and password to SMTP relay hosts. Create the file /etc/postfix/relay_password and edit it to have the following content (replace the hostname with whatever relay host your ISP is providing and use your password and login)

    smtp.bredband.net :
  2. Change permissions for the credentials file and create a map file

    # chown root:root /etc/postfix/relay_password
    # chmod 600 /etc/postfix/relay_password
    # postmap /etc/postfix/relay_password
  3. Update postfix configuration

    relayhost = [smtp.bredband.net]
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/relay_password
  4. Restart postfix

    # /etc/init.d/postfix restart
  5. Share

Control Debian daemons

Posted on the February 22nd, 2009 under Computers,Linux by

Debian and Ubuntu place startup files for all deamons in /etc/init.d. Symbolic links are then placed in /etc/rc.X to control when deamons are to be started and stopped. While it would certainly be possible to manage it manually, there are a few tools that makes life easier whenever you want to control which daemons are started by default.

Using rcconf

rcconf

Using sysv-rc-conf

sysv-rc-conf

Using update-rc.d

usage: update-rc.d [-n] [-f]  remove
       update-rc.d [-n]  defaults|multiuser [NN | sNN kNN]
       update-rc.d [-n]  start|stop NN runlvl [runlvl] [...] .
		-n: not really
		-f: force

Share

iPhone viewport size

Posted on the February 15th, 2009 under Computers,Web design by

This information is from 2007 but every time I make a web page for the iPhone I have to go and search for it.

The iPhone Safari browser does a good job of presenting just about any web page on its relatively small display. In so doing, it assumes that whatever web page it is displaying was designed for a much bigger display. To design a web page targeted for the iPhone and not have the phone shrink the web page to oblivion one must therefore add a meta tag to the head section of the HTML file:

<meta name="viewport" content="width=320, user-scalable=yes">

Share

Upgrade BIOS on Acer Aspire One

Posted on the February 15th, 2009 under Computers,Linux by

In a previous post I wrote about how easy it was to upgrade the BIOS on Asus Eee Box. It turns out that the Acer Aspire One is just as simple.

  • Go to Acer’s support pages and download the BIOS you want to use.
  • Format a USB stick with FAT32
  • Put the BIOS image file and FLASHIT.EXE (also from Acer) in the root directory. Rename the BIOS image file to ZG5IA32.FD
  • Turn off the computer, then turn it back on while pressing Fn+ESC. Release Fn+ESC after a few seconds. The power button should now be blinking. Press the power button once. This will start the BIOS flashing process.

N.B. Keep the computer connected to AC and do not interrupt the process once it has started. Also, while the above worked very well for me I can offer no guarantees.

Share

Unix epoch counter plugin for WordPress

Posted on the February 12th, 2009 under Computers,Linux by

On Friday the Unix time used by many of the computers throughout the world will tick up to 1234567890. As I have learned, most non-techies don’t understand the beauty of this. Strange.

Anyway, I thought I should have a Unix epoch time counter on the web page but was surprised that I couldn’t find a widget to do it. Not to be let down, I realised that this was the perfect opportunity to learn how to write a WordPress plugin.

The result is in the sidebar and the code can be downloaded below. Now all I have to do is wait up on Friday night (0.30 am here in Sweden) and watch the counter step up towards the magic number.

Download epoch-counter.php

Share

Patiently waiting for EOS 5D Mk II

Posted on the December 25th, 2008 under Gadgets,Photography by

Canon announced the update to the EOS 5D on September 18th, more than three months ago. Now the holiday season is all over us and there are still no EOS 5D Mk II to be found – at least not here in Sweden.

Canon recently announced some demonstration films showing off the video capability that this camera – together with some good optics – can deliver. The result is absolutely astonishing. I will have to reevaluate my assumption that I wouldn’t use the video feature.

Now I only wish that Canon could learn something from Apple. If they announce something they should have it ready for delivery. I’m getting tired of the waiting and if they don’t start shipping these babies in volumes my allocated budget may be put into use for some other gadget.

Share

Web classic: How real men start their grill

Posted on the December 12th, 2008 under Uncategorized by

This is a partial quote from early WWW sources. I would gladly give credit to someone but I don’t know who wrote the original text. I remember reading it around 1995 while I was still at University, looking at the video on the Sun computers in the lab. Nowadays just about anyone posts videos on YouTube but Goble and his colleagues were among the pioneers.



A guy named George Goble (really!!), a computer person in the Purdue University engineering department. Each year, Goble and a bunch of other engineers hold a picnic in West Lafayette, Indiana, at which they cook hamburgers on a big grill. Being engineers, they began looking for practical ways to speed up the charcoal-lighting process.

“We started by blowing the charcoal with a hair dryer,” Goble told me in a telephone interview. “Then we figured out that it would light faster if we used a vacuum cleaner.”

If you know anything about (1) engineers and (2) guys in general, you know what happened: The purpose of the charcoal-lighting shifted from cooking hamburgers to seeing how fast they could light the charcoal. From the vacuum cleaner, they escalated to using a propane torch, then an acetylene torch. Then Goble started using compressed pure oxygen, which caused the charcoal to burn much faster, because as you recall from chemistry class, fire is essentially the rapid combination of oxygen with a reducing agent (the charcoal).

By this point, Goble was getting pretty good times. But in the world of competitive charcoal-lighting, “pretty good” does not cut the mustard. Thus, Goble hit upon the idea of using – get ready – liquid oxygen.

This is the form of oxygen used in rocket engines; it’s 295 degrees below zero and 600 times as dense as regular oxygen. In terms of releasing energy, pouring liquid oxygen on charcoal is the equivalent of throwing a live squirrel into a room containing 50 million Labrador retrievers.

You can see actual photographs and a video of Goble using a bucket attached to a 10-foot-long wooden handle to dump 3 gallons of liquid oxygen (not sold in stores) onto a grill containing 60 pounds of charcoal and a lit cigarette for ignition. What follows is the most impressive charcoal-lighting I have ever seen, featuring a large fireball that according to Goble, reached 10,000 degrees Fahrenheit.

The charcoal was ready for cooking in – this has to be a world record – 3 seconds. There’s also a photo of what happened when Goble used the same technique on a flimsy A2.88 discount-store grill. All that’s left is a circle of charcoal with a few shreds of metal in it.

“Basically, the grill vaporized,” said Goble. “We were thinking of returning it to the store for a refund.”

Share