Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Inserting into a database

Thread Tools

Inserting into a database

Posts: n/a

Im currently inserting data into a database from a log file. This is
done by matching words from a log file and placing the line of the log
file into the database field. This is repeated for each log file in the

Currently I can get the information into the correct fields of the
My problem is when a word is not matched in the log file that the
script is looking through and thus, not placed into the database field.
As a result, the next line of information is not then imported into the
log file when it should be! The script should not match the previous
word - leave the field in the database blank and then carry on with the
pattern matching - filling in the remaining fields.

Current code (not working code because i think you need to have a
database to understand the problem - and im not sure how placing
working code (without database) would help!:

use strict;
use warnings;
use DBI;

my @filenames = </home/username/logs/*.log>;
foreach my $filename (@filenames)
open my $LOG, '<', $filename or die "can't open $filename: $!\n";

my ($info, $elapsed1, $fast, $elapsed2, $inversions, $elapsed3 );
if (/updates table/)
print "$_\n";
$info = $_;
if (/elapsed/)
print "[$_]\n";

if ($inversions)
$elapsed3 = $_;
elsif ($fast)
$elapsed2 = $_;
elsif ($info)
$elapsed1 = $_;
if (/FASTSEARCH|conflicting/)
print "$_\n";
$fast = $_;
if (/inversions/)
print "$_\n";
$inversions = $_;
my $dbh = DBI ->connect("dbi:Oracle:server", "database", "password")
or die "couldn't connect to database: $DBI::errstr\n";

$dbh->do("INSERT INTO TEST2 (sfd, w, sdf, hfg, rt, bv, te)
VALUES (?, ?, ?, ?, ?, ?, ?)", undef,
$filename, $info, $elapsed1, $fast, $elapsed2, $inversions,


When i change 'elsif ($fast)' to ($fast or " ") it solves the problem
(includes the field after the blank field) but it leaves the whole
column of elapsed1 blank - is this because it is in a loop and can only
do 1 thing or another but not both? Is there a way to solve this?
I have also tried changing the last elsif statement to 'else' but to no


Reply With Quote
Brian McCauley
Posts: n/a

MattJ83 wrote:

> Subject: Inserting into a database

> Current code (not working code because i think you need to have a
> database to understand the problem - and im not sure how placing
> working code (without database) would help!:

I think you have a serious problem parititioning problem. I strongly
doubt your problem has anything whatever to do with inserting to a
database. If you replaced the line of code that inserts the results of
your parsing operation into the database with a simple print statement
then your parser would still (miss-)behave exactly the same.

Also you code is erratically indented. This makes it very hard to
follow. I had to import it into an editor and hit auto-indent before I
stood a chance.

Please try it. If I'm right then please produce a mimimal but complete
(cleanly indented) program that doesn't need a database and post it
here with sample input.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase Computer Information 0 09-26-2012 09:40 AM
Syntax problem Inserting variables into database altergothen ASP .Net 2 04-19-2004 11:41 AM
inserting Checkboxlist input into database wjer ASP .Net 1 03-02-2004 06:26 AM
Retrieve ID after inserting into Access database ASP .Net 0 10-28-2003 10:07 PM