Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

read file into database

Posted on 2004-10-22
6
Medium Priority
?
188 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
Comment
Question by:Luxana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Assisted Solution

by:gripe
gripe earned 600 total points
ID: 12386826
#!/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
ID: 12386864
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
ID: 12386887
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 12387264
     
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
ID: 12387307
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:
ozo earned 1400 total points
ID: 12387363
while( <FILE> ){
     chomp;
     $sth->execute(reverse split/:/);
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

636 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