on Wed, 12 Jun 2002 10:43:14 GMT, [EMAIL PROTECTED] (Harry Jackson) wrote:
> Has anyone ever wrote a script that creates a schema for MySql or > Postgres. I am just wondering how you would have went about it. I > am going to have to write an installation type script and was > thinking about Perl for doing it. I usually use the following method (for MySQL): #! /usr/bin/perl -w use strict; use DBI; my $dbh = DBI->connect('dsn', 'user', 'password', {RaiseError => 0, PrintError => 0}); { local $/ =';'; while (<DATA>) { chomp; $dbh->do($_) or warn "Statement '$_' failed: $DBI::errstr\n"; } } $dbh->disconnect(); __DATA__ DROP TABLE IF EXISTS atable; CREATE TABLE atable ( afield VARCHAR(10) NOT NULL ); ALTER TABLE atable ADD PRIMARY KEY (aafield); DROP TABLE IF EXISTS anothertable; CREATE TABLE anothertable ( anotherfield VARCHAR(10) NOT NULL ); ALTER TABLE anothertable ADD PRIMARY KEY (anotherfield); Note that I explicitely turn of DBI's error handling, and do the error handling myself (here with a simple 'warn', but in production code this could be more elaborate). Note the deliberate error in the first 'ALTER TABLE' statement, which will be printed on the screen but will not stop the program. -- felix -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]