use strict; use warnings; use DBI; # some info my $driver = 'mysql'; # this requires DBD::mysql my $db_name = 'mydb'; my $db_user = 'user'; my $db_pass = 'pass'; my $dsn = "DBI:$driver:$db_name"; # get the source file from command line my $source_file = shift or die "Please supply source filename\n"; # connect to the db, we rely on the default "on" # of AutoCommit and PrintError my $dbh = DBI->connect($dsn, $db_user, $db_pass, {ShowErrorStatement => 1}); die $DBI::errstr unless $dbh; # prepare the statement, the number of question marks # must match the number of column to be filled in my $sql = 'INSERT INTO news (url, header, ...., state) values (?, ?, ..., ?)'; my $sth = $dbh->prepare($sql); # enter the loop after opening the source file open my $fh, '<', $source_file or die "Can't open $source_ile: $!\n"; my $i = 0; while (<$fh>) { chomp; my @column_data = split /\|/; $sth->execute(@column_data) or die "Failed to insert data at line $. (data: [@column_data]): ", $sth->errstr; $i++; } close $fh;