in reply to Inserting data in Mysql

A few notes:

  1. 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.
  2. 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.)
  3. 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.

Replies are listed 'Best First'.
Re^2: Inserting data in Mysql
by locked_user sundialsvc4 (Abbot) on Dec 03, 2010 at 13:44 UTC

    Pay particular attention to the fact that, in the example above, the question-marks are not enclosed in quotes.   If they were, they’d be seen as a literal character-string consisting of a question mark.   For some strange reason, I whacked my forehead against that quite a few times, in my earlier days.   (Thank you ... I feel so much better now ...)

    INSERT and UPDATE statements are two of the biggest reasons for having placeholders, and they’re also one of their biggest beneficiaries.   When you do these things, you’re inserting user-provided data (which might be malicious), and you’re probably doing it thousands of times, maybe under a fairly tight schedule.   So, prepare the statement-handle once, then keep re-using it.

    If you are doing many inserts, be sure to do it within a transaction.   (COMMIT periodically if it makes sense to do so.)   This tells the database server that it doesn’t have to ensure that the data has been committed to disk before the statement completes.   For an SQLite database-file, for instance, this simple concern makes a “night vs. day” difference in performance.   Naturally, there are auto_commit features available...