in reply to DBI, add fields to existing table?

I'll bet you are using MySQL.

The answer is:

$dbh->do("ALTER TABLE $dbtable ADD COLUMN (Name text)"); $dbh->do("ALTER TABLE $dbtable ADD COLUMN (Address text)");

-Andrew.


Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com

Replies are listed 'Best First'.
Re^2: DBI, add fields to existing table?
by Elijah (Hermit) on Aug 21, 2005 at 18:51 UTC
    ALTER TABLE was correct which was suggested before but the syntax I ended up using was:
    my $cmd = "ALTER TABLE $dbTable ADD (NAME text, ADDRESS text)"; my $cmd_prep = $db->prepare($cmd); unless($cmd_prep->execute()) { push(@error, "Database insertion error: ".DBI::errstr); } $cmd_prep->finish();
    prepare() and execute() is preferred over do()
      The finish() in your snippet isn't needed. It is only for fetching data when the fetch is not completed. It's true that prepare() & execute() is preferable when you are executing the same statement many times (prepare once, execute many), but if you are only executing a single statement and especially a statement like ALTER (which can't have any placeholders in it), there is no reason at all to prefer prepare() and execute() over do().