A few notes:
- Using placeholders is really the correct choice for something like this. It handles all your quoting for you, properly handles nulls, and simplifies your code. A complete win.
- Never comment out strict and warnings. If they're telling you something, you ought to first understand what they're telling you and repair the problems rather than mask any problems and plowing ahead. (Yes, there are cases where it's appropriate to turn some of it off, but when you investigate it properly, you'll find out when to do so.)
- I don't use MySQL, but I really don't think you can insert multiple records as you're trying to do.
So I rearranged your code a little, converted to use placeholders and insert the records as they're read. I didn't bother to compile and/or test, so there could easily be a few syntax errors. But this should give you a push in the right direction:
#!perl
use strict;
use warnings;
use File::DosGlob 'glob';
use DBI;
use DBD::mysql;
my $dir = "/user/data/";
$database = userdata;
$dbh = DBI->connect('dbi:mysql:userdata','root','')
or die "Connection Error: $DBI::errstr\n";
my $sth = $dbh->prepare(<<EOSQL)
or die "Can't prepare insert statement: $DBI::errstr\n";
INSERT into userdata(id, name, age, address)
values (?, ?, ?, ?)
EOSQL
chdir $dir;
for my $file (grep {-f} glob '2010-05*')
{
open my $fh, '<', $file;
while (<$fh>)
{
my ($id) = /\bid=(\d+)/;
next if not defined $id; # Can't store without a key!
my ($name) = /\bname=("(.*?)")/i;
my ($age) = /\bage=(\d+)/;
my ($add) = /\baddress=("(.*?)")/i;
$sth->execute($id, $name, $age, $add) or die $DBI::errstr;
}
}
...roboticus
When your only tool is a hammer, all problems look like your thumb.
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.