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

I'm trying to create a SQLite database...I get no errors with this code but when I go to the SQLite3 procedure and do .schema or .table, I get nothing....I would greatly appreciate a helping hand.
#! perl use strict; use warnings; use DBI; my $dbh; &do_connect(\$dbh); &create_tables($dbh); exit; sub do_connect { # my $dbh = shift or die "code_review:do_connect: must pass in +handle...$!\n"; my $driver = "SQLite"; my $database = "/home/user/bmcclintock/code_review.db"; my $username = undef; my $password = undef; my $dsn = "DBI:$driver:database=$database"; $dbh = DBI->connect( $dsn, $username, $password ); } sub create_tables { my $dbh = shift; # Delete the table if it already exists. eval { local $dbh->{PrintError} = 1; $dbh->do("DROP TABLE review") and print "review dropped\n"; $dbh->do("DROP TABLE depot") and print "depot dropped\n"; $dbh->do("DROP TABLE reviewer") and print "reviewer dropped\n"; die "error in dropping tables\n" if $@; }; my $table = "review"; $dbh->do("CREATE TABLE $table (key INTEGER PRIMARY KEY, submitter VARCHAR(50), director VARCHAR(50), change_num INTEGER, action VARCHAR(20), time DATE )") or die "code_review::create_table: + could not create <$table>...$!\n"; print "\n$table created\n"; $table = "depot"; $dbh->do("CREATE TABLE $table (change_num INTEGER PRIMARY KEY, depot VARCHAR(50) )") or die "code_review::create_table: + could not create <$table>...$!\n"; print "$table created\n"; $table = "reviewer"; $dbh->do("CREATE TABLE $table (change_num INTEGER PRIMARY KEY, reviewed_by VARCHAR(50) )") or die "code_review::create_table: + could not create <$table>...$!\n"; print "$table created\n"; $dbh->do("CREATE TRIGGER INS_TIMEENTER AFTER INSERT ON REVIEW BEGIN UPDATE REVIEW SET TIME = DATETIME('NOW','LOCALTIME') WHERE ROWID = NEW.ROWID; END"); print "\ntables created\n"; }

Replies are listed 'Best First'.
Re: create SQLite database problems
by chilledham (Friar) on Mar 06, 2012 at 19:47 UTC

    I copied your code, changed the path to the database file (your home directory doesn't exist on my machine) and it ran just fine. I then connected to the database via sqlite3 and both the .tables and .schema commands returned what I would expect (namely the list of tables and their definitions).

    What command are you using to connect to the database? If you're not targeting the specific database you created, sqlite3 will create a new database and it would indeed be empty. Do you still find a lack of tables & schema when connecting as such: sqlite3 /home/user/bmcclintock/code_review.db?

      there is a connect routine in the code sample I provided.

        I am unclear on what you mean by "when I go to the SQLite3 procedure". Can you expand on that, please?

        In my example, after the perl script runs, I used the sqlite3 command line tool to connect to the database that was created. That's where I saw the tables and schema.

Re: create SQLite database problems
by Riales (Hermit) on Mar 06, 2012 at 23:24 UTC

    This seems to work for me as well. Kind of a shot in the dark, but have you tried explicitly turning autocommit on or just explicitly calling commit at the end of everything?

    While you're at it, maybe consider setting the RaiseError flag to true as well...you never know, right? Something like:

    $dbh = DBI->connect( $dsn, $username, $password, Autocommit => 1, RaiseError => 1, );

      Just a minor point: Autocommit should be AutoCommit

      Good suggestion! Still worked in my environment (and setting AutoCommit => 0 then doing $dbh->rollback(); did indeed create an empty SQLite database).

      EDIT
      Also, DBI expects AutoCommit & RaiseError to be passed in a hash reference:

      $dbh = DBI->connect( $dsn, $username, $password, { AutoCommit => 1, RaiseError => 1} );

        Blah, that'll teach me to forgo checking documentation and rely on my memory for such things. Good catch!