package DbLibTry; use strict; use warnings; use diagnostics; use DBI; sub new { return bless {}, shift; } sub connect { my $self = shift; $self->{dbh} = DBI->connect( "DBI:SQLite:dbname=songdb.sqlite3", q{}, q{}, {RaiseError => 1} ) or return $DBI::errstr; return 1; } sub disconnect { my $self = shift; $self->{dbh}->disconnect; return 1; } sub get_all_songs { my $self = shift; my $sth = $self->{dbh}->prepare('select * from songs;'); my $rv = $sth->execute or return $DBI::errstr; return $DBI::errstr if ($rv < 0); my %hash; while (my @row = $sth->fetchrow_array) { $hash{$row[0]} = { name => $row[1], # Name of song time => $row[2], # Total time of song album => $row[3], # Album ID for song order => $row[4], # Order of song in album }; } return \%hash; } 1;