Solved

read file into database

Posted on 2004-10-22
138 Views
Last Modified: 2010-03-05
hello experts,

I need to read file into ma DBI database using perl script. A have file usage.fin which contains :

20k:w4293
20k:w4304
20k:w4324
20k:w4339
20k:w4369
1.3M:w4400
20k:w4412

also I have DBI database file with flields :

#cat space_usage
name,usage

I have DBI driver installed and I'm able to add entry to dabase with script like this:

#cat add_values
#!/usr/bin/perl

use DBI;

my $dbh = DBI->connect("DBI:CSV:f_dir=.")
        or die "Cannot connect: " . $ $DBI::errstr;
$dbh->do( "insert into space_usage values ('w2601','1550MB')");

then database :

#cat space_usage
name,usage
w2601,1550MB

--------------------------

What I need is read file usage.fin into add_values script and add all entries form file to my two databese fields. So my database will look like this:

cat space_usage
name,usage
w4293,20k
w4304,20k
w4324,20k
w4339,20k
w4369,20k
w4400,1.3M
w4412,20k

What I done until now is that I can read file into script but I need to split string "20k:w4293" into two varilables like $name and $usage and make loop with
$dbh->do( "insert into space_usage values ($name,$usage)");

I  have a look on split command but not sure how to use it in my case . I'm very beginner so maybe there is another solution how to do same result so if somebody have better way please let me know please.. I'll award points to most informative answer and best solution.

thanks

./lubo

0
Question by:Luxana
    6 Comments
     
    LVL 13

    Assisted Solution

    by:gripe
    #!/usr/bin/perl

    use DBI;

    my $dbh = DBI->connect("DBI:CSV:f_dir=.")
            or die "Cannot connect: " . $ $DBI::errstr;

    open my $file, "data.txt" or die $!;

    while (<$file>) {
         @vals = split /,/;
         $sth = $dbh->prepare('insert into space_usage values(?, ?)');
         $sth->execute(@vals);
    }
    0
     
    LVL 13

    Expert Comment

    by:gripe
    Actually, i doubt it will make a difference with DBD::CSV, but this would be better. Note the prepare outside of the while loop:

    #!/usr/bin/perl

    use DBI;

    my $dbh = DBI->connect("DBI:CSV:f_dir=.")
            or die "Cannot connect: " . $ $DBI::errstr;

    open my $file, "data.txt" or die $!;

    my $sth = $dbh->prepare('insert into space_usage values(?, ?)');

    while (<$file>) {
         @vals = split /,/;
         $sth->execute(@vals);
    }
    0
     
    LVL 10

    Author Comment

    by:Luxana
    this is not what I need !

    When I use your script my database look like this:

    cat space_usage
    name,usage
    "600k:w2171
    "
    "20k:w2708
    "
    "20k:w2739
    "
    "20k:w2876
    "
    "20k:w3036

    look above for my desire output of database.
    0
     
    LVL 84

    Expert Comment

    by:ozo
         
    Comment from gripe
    Date: 10/22/2004 07:50PM PDT
          Comment       

    #!/usr/bin/perl

    use DBI;

    my $dbh = DBI->connect("DBI:CSV:f_dir=.")
            or die "Cannot connect: " . $ $DBI::errstr;

    open my $file, "data.txt" or die $!;

    while (<$file>) {
         @vals = split /,/;
         $sth = $dbh->prepare('insert into space_usage values(?, ?)');
         $sth->execute(@vals);
    }

    Comment from gripe
    Date: 10/22/2004 08:06PM PDT
          Comment       

    Actually, i doubt it will make a difference with DBD::CSV, but this would be better. Note the prepare outside of the while loop:

    #!/usr/bin/perl

    use DBI;

    my $dbh = DBI->connect("DBI:CSV:f_dir=.")
            or die "Cannot connect: " . $ $DBI::errstr;

    open my FILE, "<data.txt" or die $!;

    my $sth = $dbh->prepare('insert into space_usage values(?, ?)');

    while( <FILE> ){
         chomp;
         @vals = split /:/;
         $sth->execute(@vals);
    }

    Comment from Luxana
    0
     
    LVL 10

    Author Comment

    by:Luxana
    ozo I noticed that you changed gripe commenta bit, separator in @cals = split to ":"

    database now looks like:

    name,usage
    600k,"w2171
    "
    20k,"w2708
    "
    20k,"w2739
    "
    20k,"w2876
    "
    20k,"w3036

    but notice that field name contain usage value and field usage contain name value. How tu make it oposite?

    0
     
    LVL 84

    Accepted Solution

    by:
    while( <FILE> ){
         chomp;
         $sth->execute(reverse split/:/);
    }
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now