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...

#!/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

In reply to Perl array into MySQL Database by rsiedl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.