Skip to content

Instantly share code, notes, and snippets.

@preaction
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preaction/3d47ac7429e8bd12488b to your computer and use it in GitHub Desktop.
Save preaction/3d47ac7429e8bd12488b to your computer and use it in GitHub Desktop.
package DbLibTry;
use Mojo::Base '-strict';
use DBI;
has dbh => sub {
DBI->connect( "DBI:SQLite:dbname=songdb.sqlite3", q{}, q{}, {RaiseError => 1} )
or die $DBI::errstr;
};
sub get_all_songs {
my $self = shift;
return $self->dbh->selectall_hashref('select * from songs;', 'id'); # Assuming your id field is called "id"
}
use DbLibTry;
my $test_dbh = DBI->connect( "DBI:SQLite:dbname=test.sqlite3" );
# Insert some test data
$test_dbh->do( "INSERT INTO songs (id, name) VALUES (1, 'hello')" );
my $obj = DbLibTry->new( dbh => $test_dbh );
$obj->get_all_songs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment