Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
more useful options
 
PerlMonks  

Class::DBI example

by autarch (Hermit)
on Jan 10, 2002 at 23:38 UTC ( [id://137877]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Save MP3 Tag's to Database with SPOPS

Class::DBI is a nice module that I've used previously. Its similar to Alzabo in terms of what it does but it is much smaller (and has many fewer features) but its still quite useful.
package Song; use base qw(Class::DBI); __PACKAGE__->set_db( 'Main', 'dbi:mysql', 'username', 'password' ); __PACKAGE__->table('Song'); __PACKAGE__->columns( All => qw( song_id title artist album year genre + ) ); __PACKAGE__->columns( Primary => 'song_id' ); package main; find sub { return unless m/\.mp3$/; my $tag = get_mp3tag($_) or return; Song->create($tag); }, @ARGV;

Replies are listed 'Best First'.
Re: Class::DBI example
by salvadors (Pilgrim) on Feb 11, 2002 at 05:16 UTC
    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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://137877]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.