LEOCHARRE::Database::Base - added methods to DBI:db
use DBI; use base 'LEOCHARRE::Database::Base'; my $dbh = connect_sqlite('/abs/path/to.db');
When used, this module adds methods to dbh handles, (DBI::db objects).
If instead you import, it just imports these subs..
argument is dbh handle and statment, returns statement handle, cached prepared in dbh object it will cache in the object, subsequent calls are not re-prepared
my $delete = sth( $dbh, 'DELETE FROM files WHERE id = ?'); $delete->execute(4); # or.. for (@ids){ sth( $dbh, 'DELETE FROM files WHERE id = ?')->execute($_); }
If the prepare fails, confess is called.
argument is statement will select and return array ref
my $users = $dbh->selectcol("SELECT user FROM users WHERE type = 'm'");
Now users has ['joe','rita','carl']
This is useful sometimes.
argument is tablename returns boolean
argument istablename returns string of table dump suitable for print to STDERR requires Data::Dumper
argument is statement or table name returns count number you MUST have a COUNT(*) if the first argument is a statement
takes 3 arguments or 1 argument, else throws an exception
my $matches = $dbh->rows_count( 'select count(*) from files' ); my $matches = $dbh->rows_count( 'files' ); #counts all entries in files table my $matches = $dbh->rows_count( 'files', size => 34 ); # all rows in files table with col 'size' == 34
closes ChildHandles that are active, finishes and undefines them. returns true + number of active handles were finished and undefined here
argument is dbh and table name returns last insert id for that table returns undef if not there
this is often only available right after an insert, if you make an insert into a table, and then into another, you cant get last insert id on the first entry.
returns boolean
returns name of DBI Driver, sqlite, mysql, etc. Currently mysql is used, sqlite is used for testing. For testing the package, you don't need to have mysqld running.
arument is dbh and table name returns boolean
argument is abs path to db returns db handle returns undef on failure
my $dbh = connect_sqlite('/home/myself/stuff.db');
args are dbname, dbuser, dbpass, hostname, if no hostname is provided, uses 'localhost' returns database handle returns undef on failure
my $dbh = connect_mysql('stuff_data','joe','joepass');
$LEOCHARRE::Database::Base::DEBUG = 1;
Leo Charre leocharre at cpan dot org
To install LEOCHARRE::Database, copy and paste the appropriate command in to your terminal.
cpanm
cpanm LEOCHARRE::Database
CPAN shell
perl -MCPAN -e shell install LEOCHARRE::Database
For more information on module installation, please visit the detailed CPAN module installation guide.