Category: AUDIO
Author/Contact Info Brent H. zzspectrez brent@bctek.com
Description: I wanted a usefull but easy example of how to use the perl DBI module. Not being familiar with the module or Databases in general, this was an easy test script.. This code is Windows based, relying on MS Access for the database. I think this might help others trying to learn how to use DBI module on windows.
#!/usr/bin/perl -w 
## dbtest.pl -                    brent@bctek.com
##
## Test script to recursively search through a series of 
## directories and create a database of the information on
## the mp3's found within the directories.
## 
## Must first create a an MS Access Database with a TABLE named MP3NAM
+E with
## the following fields. The ID field should be set as primary key and
+ autoincrement.
## All the other fields are just set as text.
## 
## ID | SONG | ARTIST | ALBUM | GENRE | YEAR | LENGTH | VBR | BITRATE 
+| FILENAME | PATH
##  |__ Primary Key


use strict;
use DBI;
use MP3::Info;
use File::Find;
use File::Basename;

my $ACCESS_DB = '//fmlypwr/database/mymp3s.mdb';     # location of ACC
+ESS DATABASE
my $mp3_path  = 'd:/';                               # TOPLEVEL direct
+ory of mp3's

my $DSN = "driver=Microsoft Access Driver (*.mdb);dbq=$ACCESS_DB";
my $dbh = DBI->connect("dbi:ODBC:$DSN", ",") or die "$DBI::errstr\n";

my $sth = $dbh->prepare('INSERT INTO MP3NAME 
                (SONG,ARTIST,ALBUM,GENRE,YEAR,LENGTH,VBR,BITRATE,FILEN
+AME,PATH) 
                VALUES (?,?,?,?,?,?,?,?,?,?)') or die "Couldn't prepar
+e statement: " . $dbh->errstr;

fileparse_set_fstype("MSWin32");

sub insert_file {
    my $type = (fileparse($_, '\.mp3'))[2];

    return unless (lc($type) eq '.mp3');

    my $mp3 = MP3::Info->new($_);
    
    if (defined $mp3) {
    my $artist = $mp3->artist;
    my $album  = $mp3->album;
    my $song   = $mp3->title;
    my $genre  = $mp3->genre;
    my $year   = $mp3->year;
    
    my $fileinfo = get_mp3info($_);
    my $time     = "$fileinfo->{TIME}";
    my $vbr      = $fileinfo->{VBR};
    my $bitrate  = $fileinfo->{BITRATE};
    
    my $fullname = $File::Find::name;
    $fullname =~ s/\//\\/g; #fix path for windows format

    $sth->execute($song,$artist,$album,$genre,$year,$time,$vbr,$bitrat
+e,$_,$fullname) 
            or die "Couldn't execute statement: " . $sth->errstr;
      }
}

print "START DIRECTORY: $mp3_path\n\n";

find({ wanted => \&insert_file }, $mp3_path); 

$dbh->disconnect;