rsiedl has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
Could anyone tell me if it is possible to insert a perl array (or hash) into a mysql table?
Here is the script I tried to test it with but it fails on the insert...
Cheers,
Reagen
Could anyone tell me if it is possible to insert a perl array (or hash) into a mysql table?
Here is the script I tried to test it with but it fails on the insert...
#!/usr/bin/perl use DBI; use strict; use warnings; my $id = 1; my @old_array = ("blah0", "blah1", "blah2"); print "Values in original array:\n"; foreach (@old_array) { if ($_) { print "\t- ", $_, "\n" } } # end-foreach my $dbh = &DB_OPEN('test','localhost','3306','user','password'); ################################# # Array Table structure # ################################# # id # array # ################################# # 1 # @array1 # # 2 # @array2 # # etc.... # ################################# my $ins_array = $dbh->prepare("insert into array_table (id,array) valu +es (?,?)"); $ins_array->execute($id,@old_array); $ins_array->finish; my $sel_array = $dbh->prepare("select array from array_table where id= +?"); $sel_array->execute($id); my (@new_array) = $sel_array->fetchrow; $sel_array->finish; print "Values in array from DB:\n"; if (@new_array) { foreach (@new_array) { if ($_) { print "\t- ", $_, "\n" } # end-if } # end-foreach } # end-if sub DB_OPEN { my ($db_name,$host_name,$port,$db_user,$db_pass,) = @_; my $database = "DBI:mysql:$db_name:$host_name:$port"; $dbh = DBI->connect($database,$db_user,$db_pass); } # end-sub exit;
Cheers,
Reagen
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Perl array into MySQL Database
by CountZero (Bishop) on Jul 21, 2004 at 11:39 UTC | |
Re: Perl array into MySQL Database
by tinita (Parson) on Jul 21, 2004 at 11:26 UTC | |
by rsiedl (Friar) on Jul 21, 2004 at 11:30 UTC | |
by knoebi (Friar) on Jul 21, 2004 at 11:43 UTC | |
Re: Perl array into MySQL Database
by hmerrill (Friar) on Jul 21, 2004 at 12:17 UTC | |
Re: Perl array into MySQL Database
by perldeveloper (Scribe) on Jul 21, 2004 at 14:18 UTC | |
by sgifford (Prior) on Jul 21, 2004 at 14:55 UTC | |
by perldeveloper (Scribe) on Jul 21, 2004 at 21:25 UTC | |
Re: Perl array into MySQL Database
by dragonchild (Archbishop) on Jul 21, 2004 at 21:54 UTC |
Back to
Seekers of Perl Wisdom