use strict; use warnings; use DBI; my $dbName = "YourDBname.sqlite"; my $import_file = "testImportFile.txt"; my %attr = ( RaiseError => 1); # auto die with error printout my $dbh = DBI->connect("dbi:SQLite:dbname=$dbName","","",\%attr) or die "Couldn't connect to database $dbName: " . DBI->errstr; open (my $fh_in, '<', $import_file) or die "cannot access file:\'$import_file\' $!\n"; #suppress Perl line number $dbh->do ("DROP TABLE IF EXISTS Data"); $dbh->do ("CREATE TABLE Data (fieldA TEXT, fieldB TEXT, fieldC TEXT)"); my $insertRow = $dbh->prepare ("INSERT INTO Data (fieldA, fieldB, fieldC) VALUES (?, ?, ?) "); $dbh->begin_work; ### Starts a transaction!! while (<$fh_in>) { next unless /^Q/; chomp; my $len = length($_); if ($len != 1017) { print "*** ERROR FOLLOWING LINE IS $len CHARS, not 1017! THIS LINE IS REJECTED\n"; print "$_\n\n"; next; } # The number of fields must match the number of columns in CREATE TABLE # or the program will stop with a fatal error! my @fields = (substr($_,1,57), substr($_,59,32), substr($_,93,25)); $insertRow->execute(@fields); print "$_\n" for @fields; print "\n"; # Just to show data for demo } $dbh->commit; ### Completes a transaction!! __END__ *** ERROR FOLLOWING LINE IS 76 CHARS, not 1017! THIS LINE IS REJECTED Q0001012345678900012345678900012345678900000000GB00000000 01234567890001234 0001012345678900012345678900012345678900000000GB00000000 01234567890001234567890123456789 ABCDEFGHIJKLMNOPQRSTUVWXY 0001012345678900012345678900012345678900000000GB00000000 01234567890001234567890123456789 ABCDEFGHIJKLMNOPQRSTUVWXY