1

Before inserting a new record in the database, how can I check if the same record is present in database table? If present, then it should throw an error message.

My insertion code:

my $sth = $dbh->prepare("INSERT into backlogs (backlogtype, name, startDate, endDate, parent_id) VALUES ('Iteration', '$iter', '" .
                                $iterations{ $iter }->{start} . " 08:00', '" .
                                $iterations{ $iter }->{end} . " 18:00', '" .
                                $project . "');");
$sth->execute() or die $DBI::errstr;
$sth->finish();
Denis Ibaev
  • 2,470
  • 23
  • 29
samio peter
  • 191
  • 2
  • 2
  • 8

4 Answers4

3

If there is any unique constraint in the table like id then code will automatic throw an exception. You just have to handle that exception and show appropriate message/error.

pan1490
  • 939
  • 1
  • 7
  • 25
0

Add a unique constraint or index to your database table. The precise syntax for this will depend on the DBMS you are using. For SQLLite for example this would like something like this:

CREATE UNIQUE INDEX backlogs_uc1 ON backlogs(backlogtype, parent_id, start_date);

Precisely which columns go in your constraint depend on your database design - you want the columns that naturally identify each row.

If you do this then your existing Perl will catch the unique constraint exception from the database and die printing the database error string.

Ross Attrill
  • 2,594
  • 1
  • 22
  • 31
0

If you cannot add constraints, here is an example that does a pre-select.

Disclaimer: This method suffers from race conditions and concurrency issues.

use DBI;

# use test; create table x ( y int not null unique );
my $dsn = "DBI:mysql:database=test;host=localhost";
my $dbh = DBI->connect($dsn, DBUSER, DBPASS);

my $sth_select = $dbh->prepare("select count(*) from x where y=?");
my $sth_insert = $dbh->prepare("insert into x (y) values (?)");

for my $i (0..9) {
    my $val = int(rand(3));
    if ($dbh->selectrow_array($sth_select, undef, $val) > 0) {
        warn "[line $i] skipped duplicate entry $val\n";
    }
    else {
        $sth_insert->execute($val);
    }
}
codnodder
  • 1,674
  • 9
  • 10
0

You can check for existence and throw error by having a code similar to this:

my $type = 'the_type';
my $name = 'the_name';

if ( $dbh->selectrow_array("SELECT COUNT(1) FROM backlogs WHERE backlogtype = '$type' and name = '$name'") ) {
    croak "Record with backlogtype '$type' and name '$name' already exists.";
}

Aside from this check, I would still recommend to have the unique constraint so that when a user tries to insert duplicate data straight into your DB without using your tool/script, it will still be caught.

Carlisle18
  • 389
  • 2
  • 11
  • may i know whats croak is? – samio peter Dec 18 '13 at 12:34
  • @sammy, Carp's croak acts similar to die but provides more information. More details here: http://www.perlmonks.org/?node_id=685452 and http://stackoverflow.com/questions/4156391/is-it-better-to-croak-or-to-die-when-something-bad-happens-in-perl. In your case, for trial, you can still use die instead of croak here. – Carlisle18 Dec 18 '13 at 12:49