Inserting into a database

Discussion in 'Perl Misc' started by MattJ83, Sep 13, 2006.

  1. MattJ83

    MattJ83 Guest

    Hi,

    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
    directory.

    Currently I can get the information into the correct fields of the
    database.
    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!:

    #!/usr/central/bin/perl
    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 );
    while(<$LOG>)
    {
    chomp;
    {
    if (/updates table/)
    {
    print "$_\n";
    $info = $_;
    }
    if (/elapsed/)
    {
    print "[$_]\n";

    if ($inversions)
    {
    $elapsed3 = $_;
    }
    elsif ($fast)
    {
    $elapsed2 = $_;
    }
    elsif ($info)
    {
    $elapsed1 = $_;
    }
    last;
    }
    }
    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,
    $elapsed3);

    }


    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
    avail.

    Thanks
     
    MattJ83, Sep 13, 2006
    #1
    1. Advertisements

  2. 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.
     
    Brian McCauley, Sep 13, 2006
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.