I'm trying to use a SQLite2 DB to mediate multiple automated build managers. Each build manager requires a unique id, but the id is only required while the manager is running (ids can be reused). The sample code below is an attempt to allocate an id to a new manager and ensure that it is unique by using transacted access to the DB. The id should normally be deleted when the code terminates.

use strict; use warnings; use DBI; my $db = new (); $db->AddBuildManager ('fred'); print "Id is $db->{myId}\n"; sub new { my $dbh = DBI->connect("dbi:SQLite2:ManSched.sqlite2", "", ""); $dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; eval { my $sth = $dbh->table_info (undef, 'public', 'managers', 'TABL +E'); my @tables = $sth->fetchrow_array (); die if @tables; $dbh->do ("CREATE TABLE managers(name VARCHAR(20) not null, id + VARCHAR(24) not null primary key)"); $dbh->commit (); }; return bless {dbh => $dbh}; } sub DESTROY { my $self = shift; my $dbh = $self->{dbh}; return if ! defined $self->{myId}; $dbh->do ("DELETE FROM managers WHERE id = '$self->{myId}'"); $dbh->disconnect (); } sub AddBuildManager { my ($self, $managerName) = @_; my $dbh = $self->{dbh}; my $sth = $dbh->prepare ("SELECT id FROM managers WHERE name = '$m +anagerName'"); $sth->execute (); my @managers = sort $sth->fetchrow_array (); for my $index (0 .. @managers) { eval { my $id = sprintf "%s-%03d", $managerName, $index + 1; die if exists ($managers[$index]) and $managers[$index] eq + $id; $dbh->do ("INSERT INTO managers(name, id) VALUES ('$manage +rName', '$id')"); $dbh->commit (); $self->{myId} = $id; }; return $self->{myId} if ! $@ and defined $self->{myId}; eval {$self->{dbh}->rollback ()}; } return; }

If I run the code three times I get the following output:

Id is fred-001
Id is fred-002
DBD::SQLite2::db do failed: column id is not unique(1) at dbdimp.c lin +e 419 at noname.pl line 55. Id is Use of uninitialized value in concatenation (.) or string at noname.pl + line 8.

What I expect to see is fred-001 reused on each run.

It looks like the DELETE doesn't at least some times, and that the SELECT doesn't return everything that is there, but I can't see why. Any ideas?


DWIM is Perl's answer to Gödel

In reply to SQLite select not returning all rows by GrandFather

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.