You are and you almost had it. Here is almost the same function with corrections.

sub insert { my ($self,$table,@values)=@_; my $query = sprintf("insert into %s values (%s);", $table,join(",",@values)); $dbh->do($query); }

Having said that I am wondering why you aren't doing something like:

: : my $sql=sprintf("insert into %s values (%s);",$table, join(",",map{ "?" } @values)); my $sth=$dbh->prepare($sql) or die $dbh->errstr; $sth->execute(@values) or die $sth->errstr;
The reason I am asking is you may get into quoting issues they way you are proceeding and the DBI interface takes care of that for you behind the scenes. Also using the "die" setup as I show it will allow you to (hopefully) catch errors.

The other thing I find curious is the lack of field names in your insert statement. I'm not a SQL guru and I haven't tried what you are proposing but I've sorta taken it on faith that it is at least a good idea to provide the field names in an insert statement.


Peter L. Berghold -- Unix Professional
Peter at Berghold dot Net
   Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.

In reply to Re: Need an array as a function parameter by blue_cowdawg
in thread Need an array as a function parameter by stonecolddevin

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.