1 10 2 20sql:
CREATE TABLE data_profile
(
file BLOB NOT NULL
)
and the perl code:
connection stuff open (IN, "<$file"); @array=Only the first line of the file is loaded into the table. Any suggestions are warmly welcomed. Thanks; print @array; $dbh->do ( "INSERT INTO data_profile (file) VALUES (?)",undef,@array); $dbh->disconnect ();
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
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.
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].
Read the entire contents into a single scalar (my $contents = do { local $/;
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