Easy(3pm) | User Contributed Perl Documentation | Easy(3pm) |
DBIx::Easy - Easy to Use DBI interface
use DBIx::Easy; my $dbi_interface = new DBIx::Easy qw(Pg template1); $dbi_interface -> insert ('transaction', id => serial ('transaction', 'transactionid'), time => \$dbi_interface -> now); $dbi_interface -> update ('components', "table='ram'", price => 100); $rows_deleted = $dbi_interface -> delete ('components', 'stock = 0'); $dbi_interface -> makemap ('components', 'id', 'price', 'price > 10'); $components = $dbi_interface -> rows ('components'); $components_needed = $dbi_interface -> rows ('components', 'stock = 0');
DBIx::Easy is an easy to use DBI interface. Currently the Pg, mSQL, mysql, Sybase, ODBC and XBase drivers are supported.
$dbi_interface = new DBIx::Easy qw(Pg template1); $dbi_interface = new DBIx::Easy qw(Pg template1 racke); $dbi_interface = new DBIx::Easy qw(Pg template1 racke aF3xD4_i); $dbi_interface = new DBIx::Easy qw(Pg template1 racke@linuxia.de aF3xD4_i); $dbi_interface = new DBIx::Easy qw(Pg template1 racke@linuxia.de:3306 aF3xD4_i);
The required parameters are the database driver and the database name. Additional parameters are the database user and the password to access the database. To specify the database host use the USER@HOST notation for the user parameter. If you want to specify the port to connect to use USER@HOST:PORT.
It is important that you commit all changes at the end of the interaction with the DBMS. You can either explicitly commit
$dbi_interface -> commit ();
or do it implicitly:
undef $dbi_interface;
sub fatal { my ($statement, $err, $msg) = @_; die ("$0: Statement \"$statement\" failed (ERRNO: $err, ERRMSG: $msg)\n"); } $dbi_interface -> install_handler (\&fatal);
If any of the DBI methods fails, either die will be invoked or an error handler installed with install_handler will be called.
By default, this module caches table structures. This can be disabled by setting $DBIx::Easy::cache_structs to 0.
The DBIx::Easy method rows fails to work with the DBD::XBase driver.
$sth = $dbi_interface -> process ("SELECT * FROM foo"); print "Table foo contains ", $sth -> rows, " rows.\n";
Processes statement by just combining the prepare and execute steps of the DBI. Returns statement handle in case of success.
$sth = $dbi_interface -> insert ('bar', drink => 'Caipirinha');
Inserts the given column/value pairs into table. Determines from the SQL data type which values has to been quoted. Just pass a reference to the value to protect values with SQL functions from quoting.
$dbi_interface -> update ('components', "table='ram'", price => 100); $dbi_interface -> update ('components', "table='ram'", price => \"price + 20");
Updates any row of table which fulfill the conditions by inserting the given column/value pairs. Scalar references can be used to embed strings without further quoting into the resulting SQL statement. Returns the number of rows modified.
$dbi_interface -> delete ('components', "stock=0");
Deletes any row of table which fulfill the conditions. Without conditions all rows are deleted. Returns the number of rows deleted.
$sth = $dbi_interface -> do_without_transaction ("CREATE DATABASE foo"); Issues a DBI do statement while forcing autocommit. This is used for statements that can't be run in transaction mode (like CREATE DATABASE in PostgreSQL).
$components = $dbi_interface -> rows ('components'); $components_needed = $dbi_interface -> rows ('components', 'stock = 0');
Returns the number of rows within table satisfying conditions if any.
$dbi_interface -> makemap ('components', 'idf', 'price'); $dbi_interface -> makemap ('components', 'idf', 'price', 'price > 10'); $dbi_interface -> makemap ('components', 'idf', '*'); $dbi_interface -> makemap ('components', 'idf', '*', 'price > 10');
Produces a mapping between the values within column keycol and column valcol from table. If an condition is given, only rows matching this condition are used for the mapping.
In order to get the hash reference to the record as value of the mapping, use the asterisk as the valcol parameter.
foreach my $table (sort $dbi_interface -> tables) { print $cgi -> h2 ('Contents of ', $cgi -> code ($table)); print $dbi_interface -> view ($table); }
Produces plain text representation of the database table table. This method accepts the following options as name/value pairs:
columns: Which columns to display.
order: Which column to sort the row after.
limit: Maximum number of rows to display.
separator: Separator inserted between the columns.
where: Display only rows matching this condition.
print $dbi_interface -> view ($table, order => $cgi -> param ('order') || '', where => "price > 0");
$dbi_interface -> insert ('transaction', id => serial ('transaction', 'transactionid'), time => \$dbi_interface -> now);
Returns representation for the current time. Uses special values of the DBMS if possible.
Stefan Hornburg (Racke), racke@linuxia.de Dennis Sch\[:o]n, ds@1d10t.de
Support for Sybase and ODBC provided by David B. Bitton <david@codenoevil.com>.
0.20
perl(1), DBI(3), DBD::Pg(3), DBD::mysql(3), DBD::msql(3), DBD::Sybase(3), DBD::ODBC(3).
2021-01-06 | perl v5.32.0 |