kilkennp
asked on
Problem with this code?
I'm trying to parse a file to a database. this is the type of information in the file:
DISCID=a50b390c
DTITLE=Various Artists / Put your Tongue to the Rail (Disc 1)
DYEAR=1999
DGENRE=Data
TTITLE0=Nothing Is True - Mae Pang
TTITLE1=It's too Late - Psyclone Rangers
TTITLE2=Three Sisters - Thorazine
TTITLE3=People Who Died - Bootleg Thorazine Nightmare
TTITLE4=Catholic Boy - Bottom
TTITLE5=City Drops into the Night - Stomaboy w/ Marrow
TTITLE6=I Want the Angel - Jen Hess
TTITLE7=Day and Night - Iota
TTITLE8=Differing Touch - Marah
TTITLE9=Crow - Del Pess
TTITLE10=Plain Division - Mia Johnson
TTITLE11=Wicked Gravity - Sesitive Pricks
I have 4 tables as follow which I think are fully normalised:
CREATE TABLE ARTIST (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(90) NOT NULL
);
CREATE TABLE SONG (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(90) NOT NULL,
tracknumber INT UNSIGNED NOT NULL,
artist_id INT UNSIGNED NOT NULL
);
CREATE TABLE ALBUM (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(90) NOT NULL,
year INT UNSIGNED NOT NULL,
genre VARCHAR(90) NOT NULL,
disc_id VARCHAR(90) NOT NULL,
UNIQUE KEY (disc_id)
);
CREATE TABLE ALBUM_SONG (
song_id INT UNSIGNED NOT NULL,
album_id INT UNSIGNED NOT NULL,
UNIQUE KEY (song_id, album_id)
);
My problem is the code below. there is something wrong with the $album_id variable below, can anyone see why? Also, how can I populate the ALBUM_SONG table?
#!usr/bin/perl
use DBI;
use strict;
use warnings;
use diagnostics;
use FileHandle;
my $db_handle = DBI->connect("dbi:mysql:da tabase=kil kennp_db2; host=mysql .cs.tcd.ie ;user=kilk ennp;passw ord=yee5ie Ph")
or die "Couldn't connect to database: $DBI::errstr\n";
my $fh = new FileHandle;
$fh->open("<$ARGV[0]") or die "could not open file\n";
open(OUTTRACKS,">makefree" ) or die "could not open trackbigrams\n";
my($album_id,$artist_id,$t itle,$year ,$genre,$d isc_id);
while (<$fh>)
{
if(/DTITLE=(.*)/)
{
my $length = length ($_);
if ($length > 8)
{
if( /DTITLE=(.*) \/ (.*)/ )
{
my $artist = $1;
$title = $2;
$db_handle->do('INSERT INTO ARTIST (name) VALUES (?);',undef,$artist);
$artist_id=($db_handle->se lectrow_ar ray("SELEC T id FROM ARTIST WHERE name=?;",undef,$artist))[0 ];
}
else
{
$title = "$1";
my $artist = "N/A";
$db_handle->do('INSERT INTO ARTIST (name) VALUES (?);',undef,$artist);
$artist_id=($db_handle->se lectrow_ar ray("SELEC T id FROM ARTIST WHERE name=?;",undef,$artist))[0 ];
}
}
elsif ($length == 8)
{
$title = "N/A";
my $artist = "N/A";
$db_handle->do('INSERT INTO ARTIST (name) VALUES (?);',undef,$artist);
$artist_id=($db_handle->se lectrow_ar ray("SELEC T id FROM ARTIST WHERE name=?;",undef,$artist))[0 ];
}
}
elsif( /DYEAR=(.*)/ )
{
my $length = length ($_);
if ($length > 7)
{
$year = $1;
}
elsif ($length == 7)
{
$year = 1;
}
}
elsif( m/DGENRE=(.*)/ )
{
my $length = length ($_);
if ($length > 8)
{
$genre = $1;
}
elsif ($length == 8)
{
$genre = "N/A";
}
}
elsif( m/DISCID=(.*)/ )
{
my $length = length ($_);
if ($length > 8)
{
$disc_id = $1;
}
elsif ($length == 8)
{
$genre = "N/A";
}
}
elsif( /TTITLE(\d*)=(.*)/ )
{
my $value = length($2);
if ($value > 0)
{
$db_handle->do("INSERT INTO SONG (name,tracknumber,artist_i d,album_id ) VALUES (?,?,?,?);",undef,$2,$1,$a rtist_id,$ album_id);
}
else
{
my $trackname = $2;
$trackname = "N/A";
$db_handle->do("INSERT INTO SONG (name,tracknumber,artist_i d,album_id ) VALUES (?,?,?,?);",undef,$trackna me,$1,$art ist_id,$al bum_id);
}
}
if( $title && $year && $genre && $disc_id )
{
$db_handle->do("INSERT INTO ALBUM (name,year,genre,disc_id) VALUES (?,?,?,?);",undef,$title,$ year,$genr e,$disc_id );
$album_id=($db_handle->sel ectrow_arr ay("SELECT id FROM ALBUM WHERE name=? AND year=? AND genre=? AND disc_id=?;",undef,$title,$ year,$genr e,$disc_id ))[0];
undef $_ for($title,$year,$genre,$d isc_id);
}
}
$fh->close();
$db_handle->disconnect();
close(OUTTRACKS);
DISCID=a50b390c
DTITLE=Various Artists / Put your Tongue to the Rail (Disc 1)
DYEAR=1999
DGENRE=Data
TTITLE0=Nothing Is True - Mae Pang
TTITLE1=It's too Late - Psyclone Rangers
TTITLE2=Three Sisters - Thorazine
TTITLE3=People Who Died - Bootleg Thorazine Nightmare
TTITLE4=Catholic Boy - Bottom
TTITLE5=City Drops into the Night - Stomaboy w/ Marrow
TTITLE6=I Want the Angel - Jen Hess
TTITLE7=Day and Night - Iota
TTITLE8=Differing Touch - Marah
TTITLE9=Crow - Del Pess
TTITLE10=Plain Division - Mia Johnson
TTITLE11=Wicked Gravity - Sesitive Pricks
I have 4 tables as follow which I think are fully normalised:
CREATE TABLE ARTIST (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(90) NOT NULL
);
CREATE TABLE SONG (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(90) NOT NULL,
tracknumber INT UNSIGNED NOT NULL,
artist_id INT UNSIGNED NOT NULL
);
CREATE TABLE ALBUM (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(90) NOT NULL,
year INT UNSIGNED NOT NULL,
genre VARCHAR(90) NOT NULL,
disc_id VARCHAR(90) NOT NULL,
UNIQUE KEY (disc_id)
);
CREATE TABLE ALBUM_SONG (
song_id INT UNSIGNED NOT NULL,
album_id INT UNSIGNED NOT NULL,
UNIQUE KEY (song_id, album_id)
);
My problem is the code below. there is something wrong with the $album_id variable below, can anyone see why? Also, how can I populate the ALBUM_SONG table?
#!usr/bin/perl
use DBI;
use strict;
use warnings;
use diagnostics;
use FileHandle;
my $db_handle = DBI->connect("dbi:mysql:da
or die "Couldn't connect to database: $DBI::errstr\n";
my $fh = new FileHandle;
$fh->open("<$ARGV[0]") or die "could not open file\n";
open(OUTTRACKS,">makefree"
my($album_id,$artist_id,$t
while (<$fh>)
{
if(/DTITLE=(.*)/)
{
my $length = length ($_);
if ($length > 8)
{
if( /DTITLE=(.*) \/ (.*)/ )
{
my $artist = $1;
$title = $2;
$db_handle->do('INSERT INTO ARTIST (name) VALUES (?);',undef,$artist);
$artist_id=($db_handle->se
}
else
{
$title = "$1";
my $artist = "N/A";
$db_handle->do('INSERT INTO ARTIST (name) VALUES (?);',undef,$artist);
$artist_id=($db_handle->se
}
}
elsif ($length == 8)
{
$title = "N/A";
my $artist = "N/A";
$db_handle->do('INSERT INTO ARTIST (name) VALUES (?);',undef,$artist);
$artist_id=($db_handle->se
}
}
elsif( /DYEAR=(.*)/ )
{
my $length = length ($_);
if ($length > 7)
{
$year = $1;
}
elsif ($length == 7)
{
$year = 1;
}
}
elsif( m/DGENRE=(.*)/ )
{
my $length = length ($_);
if ($length > 8)
{
$genre = $1;
}
elsif ($length == 8)
{
$genre = "N/A";
}
}
elsif( m/DISCID=(.*)/ )
{
my $length = length ($_);
if ($length > 8)
{
$disc_id = $1;
}
elsif ($length == 8)
{
$genre = "N/A";
}
}
elsif( /TTITLE(\d*)=(.*)/ )
{
my $value = length($2);
if ($value > 0)
{
$db_handle->do("INSERT INTO SONG (name,tracknumber,artist_i
}
else
{
my $trackname = $2;
$trackname = "N/A";
$db_handle->do("INSERT INTO SONG (name,tracknumber,artist_i
}
}
if( $title && $year && $genre && $disc_id )
{
$db_handle->do("INSERT INTO ALBUM (name,year,genre,disc_id) VALUES (?,?,?,?);",undef,$title,$
$album_id=($db_handle->sel
undef $_ for($title,$year,$genre,$d
}
}
$fh->close();
$db_handle->disconnect();
close(OUTTRACKS);
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.