frazap has asked for the wisdom of the Perl Monks concerning the following question:

I'm opening an sqlite database with
my $dsn = "dbi:SQLite:$dbname"; $dbh = DBI->connect( $dsn, undef, undef, { PrintError => 0, RaiseError => 1, AutoCommit => 1, sqlite_see_if_its_a_number => 1, sqlite_unicode => 1 } ) or die("Cannot connect to the database: $DBI::errstr ($DBI::er +r) \n"); my $self = { dbh => $dbh }; die "dbh undef " unless ($dbh); # print "Dbman_sqlite->new\n"; $self->{dbh}->trace(1, *STDOUT);

Then I prepare some statements

my $sql = "INSERT INTO authors (name, firstname) VALUES (?, ?);"; $self->{st_insert_authors} = $self->{dbh}->prepare($sql); $sql= "UPDATE papers set year=? WHERE rero_id=?"; $self->{st_update_papers} = $self->{dbh}->prepare($sql); $sql = "SELECT authorid FROM authors WHERE name=? AND firstname=?" +; $self->{st_select_authors} = $self->{dbh}->prepare($sql); ...
I use the last statement with
$self->{st_select_authors}->execute($name, $firstname) or die $se +lf->{st_select_authors}->errstr; my $val_ar = $self->{st_select_authors}->fetchall_arrayref( {} ); my $foundid; die Dumper $val_ar; #gives an empty array ref
The problem is that the name + firstname value are in my table... I tried to test if $self->{st_select_authors}->{Active} gives an indication but it seems to be false, even in the case where a select statement works correctly.

The trace is

:\docs\OA\entrees_liste>build_rerodb.pl -f out_181106.txt DBI::db=HASH(0x2bc47c4) trace level set to 0x0/1 (DBI @ 0x0/0) in +DBI 1.636-ithread (pid 7692) <- prepare('INSERT INTO authors (name, firstname) VALUES (?, ?);') += ( DBI::st=HASH(0x2bc4d4c) ) [1 items] at Collabs_db.pm line 40 <- prepare('UPDATE papers set year=? WHERE rero_id=?')= ( DBI::st= +HASH(0x2bc6294) ) [1 items] at Collabs_db.pm line 42 <- prepare('SELECT authorid FROM authors WHERE name=? AND firstnam +e=?')= ( DBI::st=HASH(0x2bc645c) ) [1 items] at Collabs_db.pm line 44 <- prepare('DELETE FROM papers WHERE rero_id=?')= ( DBI::st=HASH(0 +x2bc65f4) ) [1 items] at Collabs_db.pm line 46 <- prepare('INSERT INTO papers (rero_id, doi, year) VALUES (?,?, ? +)')= ( DBI::st=HASH(0x2bc678c) ) [1 items] at Collabs_db.pm line 48 <- prepare('SELECT rero_id FROM papers WHERE rero_id=?')= ( DBI::s +t=HASH(0x2bc6924) ) [1 items] at Collabs_db.pm line 50 <- prepare('DELETE FROM writtenby WHERE rero_id=?')= ( DBI::st=HAS +H(0x2bc6abc) ) [1 items] at Collabs_db.pm line 52 <- prepare('SELECT authorid FROM writtenby WHERE rero_id=?')= ( DB +I::st=HASH(0x2bc6c54) ) [1 items] at Collabs_db.pm line 54 <- begin_work= ( 1 ) [1 items] at Collabs_db.pm line 294 <- execute("11350")= ( '0E0' ) [1 items] at Collabs_db.pm line 70 <- fetchall_arrayref(HASH(0x2aedef4))= ( [ HASH(0x2bbed1c) ] ) [1 +items] row1 at Collabs_db.pm line 71 <- execute(undef, 11350)= ( 1 ) [1 items] at Collabs_db.pm line 76 Use of uninitialized value $year in concatenation (.) or string at Col +labs_db.pm line 77, <$FH> line 1. <- execute(11350)= ( '0E0' ) [1 items] at Collabs_db.pm line 85 <- fetchall_arrayref(HASH(0x2587f34))= ( [ HASH(0x2bba87c) ] ) [1 +items] row1 at Collabs_db.pm line 86 <- execute("Normand", " Bruce")= ( '0E0' ) [1 items] at Collabs_db +.pm line 99 <- fetchall_arrayref(HASH(0x2505b94))= ( [ ] ) [1 items] at Collab +s_db.pm line 100 $VAR1 = [];

Thanks for any help !

François

Replies are listed 'Best First'.
Re: sqlite: select statement not working
by hippo (Archbishop) on Nov 09, 2018 at 12:11 UTC

    The problem is that the name + firstname value are in my table...

        <- execute("Normand", " Bruce")= ( '0E0' ) [1 items] at Collabs_db.pm line 99

    Are you sure that the firstname has that leading space?

      Bravo...

      I was extracting with

      ( $last, $first ) = ( $name =~ /^(.+),(.+)*/ );
      And I should have used
      ( $last, $first ) = ( $name =~ /^(\w+),\s*(\w+)*/ );

      Thanks !

      Update

      And in fact, the above is not working except in simple case where first name has no space in it.
      ($name, $firstname) = ($value=~/(.+),\s*(.+)*/);
      works better since it takes first name like "J. F.", "Tim Jr.", or norwegian name with funny things on some character.

      F.