#!/bin/perl use strict; use warnings; use DBI; my $dbh = DBI->connect('dbi:SQLite2:dbname=primes.db','','',{RaiseError=>1}); my $cnt = 0; # 'number' has to be text because of length $dbh->do('CREATE TABLE primes (id INTEGER, number TEXT)'); for (1..15) { $cnt = add_primes($dbh,$cnt,"primes$_.txt") } sub add_primes { my ($db, $c, $filename) = @_; open my $file, '<', $filename or die("Can't read '$filename': $!"); print STDERR "Adding primes from $filename\n"; #first two lines are not data, skip them for (1..2) { <$file> } eval { $db->begin_work; my $sth = $db->prepare('INSERT INTO primes (id,number) VALUES (?,?)'); while (<$file>) { foreach ( split(/\s+/,$_) ) { $sth->execute(++$cnt,$_) } } $db->commit; }; if ($@) { print STDERR "Died while processing prime #$cnt, with error:\n$@"; exit; } return $cnt; }