perl and mysql parse a text file
Anonymous Monk
created: 2006-02-03 09:54:47
Hi all, I am having a few problems - not surprising. I want to insert a complete text in a mysql blob row. Simply the text file is:
1   10
2   20
sql:
CREATE TABLE data_profile
(
        file  BLOB NOT NULL
 )
and the perl code:
connection stuff

open (IN, "<$file");
@array=;

print @array;

$dbh->do ( "INSERT INTO data_profile (file) VALUES (?)",undef,@array);

$dbh->disconnect ();
Only the first line of the file is loaded into the table. Any suggestions are warmly welcomed. Thanks
Re: perl and mysql parse a text file
created: 2006-02-03 10:21:17

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

Re^2: perl and mysql parse a text file
created: 2006-02-03 10:29:29
this would create a separate DB row for each file row? Is it possible to store the entire file in a single row?
Re^3: perl and mysql parse a text file
created: 2006-02-03 11:17:20

Well, if you say "parse" a text file and then use an "@array", it seemed logical to assume that you wanted separate records.

With this new piece of information at hand, Fletch's post meets your needs.

I recommend you read this useful node and this insightful article (off site) before you post your next request.

Re: perl and mysql parse a text file
created: 2006-02-03 10:24:03
Assuming that you use DBI, the code should look like this:
my $sth=$dbh->prepare("INSERT INTO data_profile VALUES (?));
while(){
   $sth->execute($_)or die $dbh->errstr;
}
__DATA__
1   10
2   20
Check the docs on [http://search.cpan.org/~timb/DBI-1.48/DBI.pm|DBI].
Re: perl and mysql parse a text file
created: 2006-02-03 10:37:52

Read the entire contents into a single scalar (my $contents = do { local $/; };) 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
created: 2006-02-03 10:55:59

Fletch's answer above is a good way to go. If you need the rows in an array for some other reason, you can always join them before you insert, something like this:

@array = ;
# Other stuff with @array ...
$sth = $dbh->prepare('insert into data_profile (file) values (?)');
$sth->execute(join '' => @array);

[id://149675|Do not rebuke them with harsh words ... but rather lead them gently - with URLs - so that they may learn wisdom.]

perlmonks.org content © perlmonks.org and Anonymous Monk, Fletch, idle, rnahi, VSarkiss

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03