I am trying to parse fields from multiple files in a directory and then insert them in mysql database.
For example:
File format:
id=22 name="Mark Henry" age=35 address="82, Linking road, Parkplaza, 56532"
idno=22 name="Jack Henry" age=28 address="10, Sudon village, Brownie, 3251"
Mysql table:
id name age address
my code:
#!perl
#use strict;
#use warnings;
use File::DosGlob 'glob';
use DBI;
use DBD::mysql;
my $dir = "/user/data/";
my @data;
$database = userdata;
$dbh = DBI->connect('dbi:mysql:userdata','root','') or die "Connection
+ Error: $DBI::errstr\n";
chdir $dir;
for my $file (grep {-f} glob '2010-05*')
{
open my $fh, '<', $file;
while (<$fh>)
{
if (my ($id) = /\bid=(\d+)/)
{
push @ids, $id;
}
if (my ($name) = /\bname=("(.*?)")/i)
{
push @names, $name;
}
if (my ($age) = /\bage=(\d+)/)
{
push @ages, $age;
}
if (my ($add) = /\baddress=("(.*?)")/i)
{
push @adds, $add;
}
}
$ids1 = join(' ',@ids);
$name1 = join(' ',@names);
$age1 = join('',@ages);
$add1 = join('',@adds);
$query = "INSERT into `userdata` ( `id`, `name`, `age`, `address`) val
+ues ('$id1', '$nam1', '$age1', '$add1')";
$dbh->do($query);
}
This script is not inserting all values of corresponding fields in mysql database.
Please help.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.