Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

CRUD operations with CUBRID Perl Driver


Here is a small Perl program which creates a foo table, inserts a couple of records, then retrieves all records from that table.

To successfully run this program, make sure you have CUBRID and the Perl driver installed. In this tutorial I am using CUBRID 8.4.3 on Ubuntu 12.04 x64. For this example I have created the demodb database. If you want to have all this installed and configured for you automatically, consider using a virtual machine. The following tutorial will show you how to do it:

For this tutorial first create an empty perl file.

$ nano cubrid_perl_crud.pl

Then save the following content into this cubrid_perl_crud.pl file.

use strict;
use DBI;

# Connect to the database.
my $dbh = DBI->connect (
    "DBI:cubrid:database=demodb;host=localhost;port=33000", "dba", "", 
    {RaiseError => 1, AutoCommit => 1});

# Drop table 'foo'. This may fail, if 'foo' doesn't exist.
# Thus we put an eval around it.
$dbh->do("DROP TABLE IF EXISTS foo");

# Create a new table 'foo'. This must not fail, thus we don't
# catch errors.
$dbh->do("CREATE TABLE foo (id INTEGER, name VARCHAR(20))");

# INSERT some data into 'foo'. 
$dbh->do("INSERT INTO foo VALUES (1, 'Tim')");

# Same thing, but using placeholders
$dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen");

# Now retrieve data from the table.
my $sth = $dbh->prepare("SELECT * FROM foo");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
    print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
}
$sth->finish();

# Disconnect from the database.
$dbh->disconnect();

When you execute it, you will see the following output:

$ perl cubrid_perl_crud.pl
Found a row: id = 1, name = Tim
Found a row: id = 2, name = Jochen
comments powered by Disqus
Page info
viewed 4714 times
translations en
Author
posted last year by
Esen Sagynov
Contributors
updated last year by
View revisions
Share this article