in reply to SQLite select won't return SELECTed records

Are you aware that Perl and DBI both supply ample error checking methods that could help you simplify your code vastly?

my $s= 'DBI'; eval("use $s;"); if ($@) { die "$0: ERROR: $s not installed.\n"; }

can be replaced by simply

use DBI;

Also, you don't need to load the DBD module for the database. DBI will do that for you.

If you want to check programmer usage of your subroutines, see die and/or Carp::croak for how to replace

if (($cat) and (len($cat)==0)) { $s="$procname ERROR: Category was not provided. Cannot update data +base."; writeerr($s); exit 1; }

by them.

Also, you should change your connect command for DBI to be

my $dbh= DBI->connect($dsn, $user, $password, { RaiseError => 1, Print +Error => 0 });

That way, DBI will automatically notify you of all database errors and stop the program.

As your program runs, it seems to me that the database table is left empty. Maybe consider re-checking that the rows actually get filled.

For printing the results of a SELECT statement, I wrote DBIx::RunSQL::format_results, which outputs the results in table form. Maybe you can adapt the code from the routine to your needs.

Replies are listed 'Best First'.
Re^2: SQLite select wont' return records
by bulrush (Scribe) on Oct 16, 2014 at 11:59 UTC
    I can't use die because all errors need to be logged to a file. My writeerr() routine usually logs errors to a file but I simplified it for this post to only do a print.

    Also, the table was not created with a username or password. So why would I need them? This is a table stored in a file in the same dir as the Perl program.

    And I thought the docs said that RaiseError was on by default.

    Ok I added the RaiseError and PrintError options where you suggested. I got no errors printed and the same thing happens: In doaddsqlite() no records are selected so dupe records are added to the table. dbish still shows there are records in the table. I double checked.

      Either reopen STDERR to the file or use File::Tee to duplicate the output to a file if you want errors to (also) go to a file.

      If you don't need a username, don't pass one in. Also note that SQLite has no concept of permissions at all.

        I didn't mean SQLite permissions, I meant unix permissions for the test.db file.
      I can't use die because all errors need to be logged to a file.

      Actually, no. Think of die as though it were an exception throw and use eval to catch errors:

      eval { ... return 1; } or do { my $err = $@; WriteErr("$err"); };
      Perl is the programming world's equivalent of English