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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    421
  2. wjer
    Replies:
    1
    Views:
    545
    Alvin Bruney [MVP]
    Mar 2, 2004
  3. altergothen
    Replies:
    2
    Views:
    396
    Hans Kesting
    Apr 19, 2004
  4. Arnold Peters
    Replies:
    1
    Views:
    473
    Paul Lutus
    Oct 20, 2004
  5. Replies:
    0
    Views:
    421
Loading...

Share This Page