On 05/12/2005 04:50 AM, Rachel Llorenna said:

Use SHOW COLUMNS on the table first; check if the desired column name
is in there, or ALTER TABLE as necessary. I think there is a MySQL-specific command you can probably use; IF NOT .. clause, but that's bad practise. It's probably better to use SHOW COLUMNS, since it will allow you to easily adopt different RDBMS's. Really makes DBI's strengths shine. :)

Do you mean column_info()?

http://search.cpan.org/dist/DBI/DBI.pm#Database_Handle_Methods

On 5/12/05, Ing. Branislav Gerzo <[EMAIL PROTECTED]> wrote:

I'm making script, and I'd like add column, if column not exist. My
DBS is MySQL, and I didn't find direct SQL command for this. Is
there some workaround for this ?

I'd do something like this:

  my $sth = $dbh -> column_info( undef, $sSchema, $sTable, $sColumn );
  my @sColumnInfo = ();
  eval { # The eval isn't needed if RaiseError == 0.
    @sColumnInfo = $sth -> fetchrow_array() || ();
  };
  if ( ! @sColumnInfo ) {
    # The ALTER TABLE statement is likely to be RDBMS specific.
    $dbh -> do( "ALTER TABLE my_table ADD my_column VARCHAR(100)" );
  }

--
Mac :})
** I usually forward private questions to the appropriate mail list. **
Ask Smarter: http://www.catb.org/~esr/faqs/smart-questions.html
Give a hobbit a fish and he eats fish for a day.
Give a hobbit a ring and he eats fish for an age.

Reply via email to