Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: perl and mysql parse a text file
by rnahi (Curate) on Feb 03, 2006 at 15:21 UTC

    If your query requires one parameter, and you are passing an array, it's correct that only the first is inserted.

    What you need is to prepare once aand execute many times.

    my $sth = $dbh->prepare( "INSERT INTO data_profile (file) VALUES (?)") +; for (@array) { $sth->execute($_); }

    Alternatively, you can create a multiple insert query.

    # assume that @array contains qw(abc def ghi); my $query = 'INSERT INTO data_profile (file) VALUES '; $query .= join(',', map {'(?)'} @array); # now $query will be 'INSERT INTO data_profile (file) VALUES (?),(?),( +?)' $dbh->do($query, @array);

    HTH

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: perl and mysql parse a text file
by Fletch (Bishop) on Feb 03, 2006 at 15:37 UTC

    Read the entire contents into a single scalar (my $contents = do { local $/; <FILE> };) and pass that to the database using an insert statement you've prepared. See the Handling BLOB / LONG / Memo Fields in the DBI documentation.

Re: perl and mysql parse a text file
by VSarkiss (Monsignor) on Feb 03, 2006 at 15:55 UTC
Re: perl and mysql parse a text file
by idle (Friar) on Feb 03, 2006 at 15:24 UTC
    Assuming that you use DBI, the code should look like this:
    my $sth=$dbh->prepare("INSERT INTO data_profile VALUES (?)"); while(<DATA>){ $sth->execute($_)or die $dbh->errstr; } __DATA__ 1 10 2 20
    Check the docs on DBI.