Programming the Perl DBI: Database programming with Perl

Programming the Perl DBI: Database programming with Perl book cover

Programming the Perl DBI: Database programming with Perl

Author(s): Tim Bunce (Author), Alligator Descartes (Author)

  • Publisher: O′Reilly
  • Publication Date: 25 Feb. 2000
  • Edition: Illustrated
  • Language: English
  • Print length: 366 pages
  • ISBN-10: 9781565926998
  • ISBN-13: 9781565926998

Book Description

One of the greatest strengths of the Perl programming language is its ability to manipulate large amounts of data. Database programming is therefore a natural fit for Perl, not only for business applications but also for CGI-based web and intranet applications.

The primary interface for database programming in Perl is DBI. DBI is a database-independent package that provides a consistent set of routines regardless of what database product you use–Oracle, Sybase, Ingres, Informix, you name it. The design of DBI is to separate the actual database drivers (DBDs) from the programmer’s API, so any DBI program can work with any database, or even with multiple databases by different vendors simultaneously.

Programming the Perl DBI is coauthored by Alligator Descartes, one of the most active members of the DBI community, and by Tim Bunce, the inventor of DBI. For the uninitiated, the book explains the architecture of DBI and shows you how to write DBI-based programs. For the experienced DBI dabbler, this book reveals DBI’s nuances and the peculiarities of each individual DBD.

The book includes:

    An introduction to DBI and its design How to construct queries and bind parameters Working with database, driver, and statement handles Debugging techniques Coverage of each existing DBD A complete reference to DBI

    This is the definitive book for database programming in Perl.

    Editorial Reviews

    Amazon Review

    The interestingly named Alligator is a database programmer and Perl module developer. Tim Bunce wrote DBI–the Perl DataBase Interface. DBI is a series of Perl modules providing a standard API to a variety of database engines. Perl DBI is a natural choice for dynamically generating Web pages from databases. DBI’s greatest strengths are portable code and the concealment of proprietary database interfaces.

    The first half of Programming The Perl DBI covers non-DBI database types, pros and cons and data handling. Perl is used to create, explain and work with these. It’s assumed that the reader is a competent enough Perl programmer to write the scripts as well as follow them. The point is to familiarise you with database technologies.

    Having dealt with SQL and other database related issues in chapter three, the authors introduce the DBI, demonstrate database connection and error handling in chapter four. The last four chapters cover database interaction, advanced DBI features, ODBC and finally shell and database proxying. Three appendices cover the DBI specification, driver and database characteristics and the ASLaN Sacred Site Charter. (The authors are megalithic site aficionados and use UK megalithic sites as database examples throughout the book).

    Descartes and Bunce’s book is a rare combination–an entertaining and accessible guide to using Perl DBI that’s also a readable and often amusing introduction to database technology.–Steve Patient

    Review

    Select Guide Rating

    From the Publisher

    The primary interface for database programming in Perl is DBI. Programming the Perl DBI is coauthored by Alligator Descartes, one of the most active members of the DBI community, and by Tim Bunce, the inventor of DBI. The book explains the architecture of DBI, shows you how to write DBI-based programs, and reveals both DBI’s nuances and the peculiarities of each individual DBD. This is the definitive book for database programming in Perl.

    About the Author

    Alligator Descartes has been an itinerant fiddler with computers from a very early age ruined only by obtaining a BSc in Computer Science from the University of Strathclyde, Glasgow. His computing credits include several years of Oracle DBA work, multi-user Virtual Reality servers, high-performance 3D graphics programming and several Perl modules. His spare time is spent trudging around Scotland looking for stone circles and Pictish symbol stones to photograph. Alligator Descartes is not his real name. Tim Bunce developed and released the first version of the DBI and DBD::Oracle modules in 1994. Just as Perl5.000 was released. Since then he has developed the DBI into the most popular database access technology for Perl. Tim has been an active perl5-porter since 1994, contributing to the development of the Perl language and many of its core modules such as DynaLoader, MakeMaker, and Exporter. He was responsible for building and releasing maintenance versions of Perl from 5.004_01 thru to 5.004_04. He is also the author and co-maintainer of the Perl Module List. Tim is the Technical Director of the Ingram Group where he designs and develops large scale data processing, storage, and reporting applications in Perl. In 1998 was recognized by British Telecom for his role in the launch of their Call Management Information service, a system implemented in Perl.

    Excerpt. © Reprinted by permission. All rights reserved.

    Chapter 4 – Programming with the DBI

    In this chapter, we’ll discuss in detail the actual programming interface defined by the DBI module. We’ll start with the very architecture of DBI, continue with explaining how to use the handles that DBI provides to interact with databases, then cover simple tasks such as connecting and disconnecting from databases. Finally, we’ll discuss the important topic of error handling and describe some of the DBI’s utility methods and functions. Future chapters will discuss how to manipulate data within your databases, as well as other advanced functionality.

    DBI Architecture
    The DBI architecture is split into two main groups of software: the DBI itself, and the drivers. The DBI defines the actual DBI programming interface, routes method calls to the appropriate drivers, and provides various support services to them. Specific drivers are implemented for each different type of database and actually perform the operations on the databases.

    Therefore, if you are authoring software using the DBI programming interface, the method you use is defined within the DBI module. From there, the DBI module works out which driver should handle the execution of the method and passes the method to the appropriate driver for actual execution. This is more obvious when you recognize that the DBI module does not perform any database work itself, nor does it even know about any types of databases whatsoever.

    Under this architecture, it is relatively straightforward to implement a driver for any type of database. All that is required is to implement the methods defined in the DBI specification,[1] as supported by the DBI module, in a way that is meaningful for that database. The data returned from this module is passed back into the DBI module, and from there it is returned to the Perl program. All the information that passes between the DBI and its drivers is standard Perl datatypes, thereby preserving the isolation of the DBI module from any knowledge of databases.

    The separation of the drivers from the DBI itself makes the DBI a powerful programming interface that can be extended to support almost any database available today. Drivers currently exist for many popular databases including Oracle, Informix, mSQL, MySQL, Ingres, Sybase, DB2, Empress, SearchServer, and PostgreSQL. There are even drivers for XBase and CSV files.

    These drivers can be used interchangeably with little modification to your programs. Couple this database-level portability with the portability of Perl scripts across multiple operating systems, and you truly have a rapid application development tool worthy of notice.

    Drivers are also called database drivers, or DBDs, after the namespace in which they are declared. For example, Oracle uses DBD::Oracle, Informix uses DBD::Informix, and so on. A useful tip in remembering the DBI architecture is that DBI can stand for DataBase Independent and DBD can stand for DataBase Dependent.

    Because DBI uses Perl’s object-orientation features, it is extremely simple to initialize DBI for use within your programs. This can be achieved by adding the line:

    use DBI;

    to the top of your programs. This line locates and loads the core DBI module. Individual database driver modules are loaded as required, and should generally not be explicitly loaded.

    Handles
    The DBI defines three main types of objects that you may use to interact with databases. These objects are known as handles. There are handles for drivers, which the DBI uses to create handles for database connections, which, in turn, can be used to create handles for individual database commands, known as statements. Figure 4-3 illustrates the overall structure of the way in which handles are related, and their meanings are described in the following sections.

    Driver Handles
    Driver handles represent loaded drivers and are created when the driver is loaded and initialized by the DBI. There is exactly one driver handle per loaded driver. Initially, the driver handle is the only contact the DBI has with the driver, and at this stage, no contact has been made with any database through that driver.

    The only two significant methods available through the driver handle are data_sources(), to enumerate what can be connected to, and connect(), to actually make a connection. These methods are more commonly invoked as DBI class methods, however, which we will discuss in more detail later in this chapter.

    Since a driver handle completely encapsulates a driver, there’s no reason why multiple drivers can’t be simultaneously loaded. This is part of what makes the DBI such a powerful interface.

    For example, if a programmer is tasked with the job of transferring data from an Oracle database to an Informix database, it is possible to write a single DBI program that connects simultaneously to both databases and simply passes the data backwards and forwards as needed. In this case, two driver handles would be created, one for Oracle and one for Informix. No problems arise from this situation, since each driver handle is a completely separate Perl object.

    Within the DBI specification, a driver handle is usually referred to as $drh.

    Driver handles should not normally be referenced within your programs. The actual instantiation of driver handles happens “under the hood” of DBI, typically when DBI->connect() is called.

    Database Handles
    Database handles are the first step towards actually doing work with the database, in that they encapsulate a single connection to a particular database. Prior to executing SQL statements within a database, we must actually connect to the database. This is usually achieved through the DBI’s connect() method:

    $dbh = DBI->connect( $data_source, … );

    The majority of databases nowadays tend to operate in a multiuser mode, allowing many simultaneous connections, and database handles are designed accordingly. An example might be if you wanted to write a stock-monitoring program that simultaneously monitored data in tables within different user accounts in the database. A DBI script could make multiple connections to the database, one for each user account, and execute SQL statements on each. Database handles are completely encapsulated objects, meaning that transactions from one database handle cannot “cross-over” or “leak” into another.

    View on Amazon

    电子书代发PDF格式价格30我要求助
    未经允许不得转载:Wow! eBook » Programming the Perl DBI: Database programming with Perl