in reply to Class::DBI example
in thread Save MP3 Tag's to Database with SPOPS

And, if you're using MySQL then the example can be even smaller:
package Song;

use base qw(Class::DBI::mysql);

__PACKAGE__->set_db( 'Main', 'dbi:mysql', 'username', 'password' );

__PACKAGE__->set_up_table('Song');

package main;
...
No need to declare your columns, as it will go ask the database for them. I keep meaning to find out how to do this for other databases, and roll them in ...

Tony

Replies are listed 'Best First'.
Re: Re: Class::DBI example
by lachoy (Parson) on Feb 11, 2002 at 12:56 UTC

    Hi Tony,

    I created a module to do this for SPOPS. I've tested this on MySQL, PostgreSQL and DB2/AS400 thru ODBC. Basically, all you need to do is issue a dummy query against a table so DBI can grab the metadata. Here's an example:

    my $dbh = DBI->connect( ... ) || die "Error connecting: $DBI::errstr"; $dbh->{RaiseError} = 1; my $sql = "SELECT * FROM $table where 1 = 0"; my ( $sth ); eval { $sth = $dbh->prepare( $sql ); $sth->execute; }; if ( $@ ) { die "Cannot fetch column info: $@" } my @column_names = @{ $sth->{NAME} }; my @column_types = @{ $sth->{TYPE} };

    I think most DBDs support this basic metadata. Whenever I've found one that didn't support both of these (like DBD::ASAny at one time), I've harrangued the author and always gotten a quick response :-)

    Chris
    M-x auto-bs-mode