Hi all,
I've run into a bit of a problem with bind params using the DBI. In short, the problem is: how do i get a DB defined default value into a column, using an insert query that is expecting a bound value for that column?

Here is a much more detailed explanation of what im trying to accomplish. Lets say i have a table defined by:
CREATE TABLE blah ( A INT, B INT NOT NULL DEFAULT 0 )
And i create a query to write to this table, such as:
my $query = "INSERT INTO blah SET A = ?, B = ? "; my $query_handle = $db_handle->prepare($query);
Then i have an array of hashrefs, each containing 1 row of values to insert, such as
my @to_insert = ( { 'A' => 1, 'B' => 1 }, { 'B' => 2 }, { 'A' => 3 }, );
Now i have a loop to go through and insert all of these rows. When i wasnt using bind params, if a column doesnt have a value, i just dont mention it in the query, and the DB takes care of inserting a default value. But when using bind params, i cant figure out a way to get the DB to insert the default value, without creating a separate query for each combination of fields to insert.

For this trivial example, there are only 3 queries, but in general, if there are n columns, there are (2^n - 1) possible column combinations, and i dont want to have to create each one.

I dont even know if this is possible, but any insight would be great.


In reply to DBI bind params & column defaults by shemp

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.