ScOut3R has asked for the wisdom of the Perl Monks concerning the following question:

Hey There Monks, i'm wokring on a small script which interacts with an sqlite database. I'd like to make a function which inserts values in a certain table. Here's the function:
sub insert_element { my $table = pop @_; my @values = @_; $db->do("insert into '$table' values ('@values')") or my $error = +$db->errstr; if (defined($error)) { die syslog('info',"insert: $error +"); } }
I have problems with the @values array, because the $db->do function wont split it into separate values. Is there a good way to separate it automatically? I'd like to insert full rows, so i'll pass as many values to this function as many columns are in the table. Thank You for your help!

Replies are listed 'Best First'.
Re: DBI Sqlite insert function
by olus (Curate) on Jun 30, 2008 at 18:03 UTC
      Ah sorry, i meant to split @values into separate values.

        From what I understand from your post, you want to convert the array @values to a string that has the array's values comma separated, as that would be the correct SQL syntax. So, if you have an array:

        @values = ( 1, 2, 3 );

        and you want to put that into the SQL query you'll have to join the values:

        my $str_values = join(', ', @values); # $str_values is now '1, 2, 3' ... $db->do("insert into $table values ($str_values)") ... ...

        Still, I strongly advise the reading of the info on placeholders.