Supplemental files and examples for this book can be found at http://examples.oreilly.com/9781565926998/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Preface
The DBI is the standard database interface for the Perl programminglanguage. The DBI is database-independent, which means that it canwork with just about any database, such as Oracle, Sybase, Informix,Access, MySQL, etc.
While we assume that readers of this book have some experience withPerl, we dont assume much familiarity with databasesthemselves. The book starts out slowly, describing different types ofdatabases and introducing the reader to common terminology.
This book is not solely about the DBIit also concerns the moregeneral subject of storing data in and retrieving data from databasesof various forms. As such, this book is split into two related, butstandalone, parts. The first part covers techniques for storing andretrieving data without the DBI, and the second, much larger part,covers the use of the DBI and related technologies.
Throughout the book, we assume that you have a basic grounding inprogramming with Perl and can put together simple scripts withoutinstruction. If you dont have this level of Perl awareness, wesuggest that you read some of the Perl books listed in .
Once youre ready to read this book, there are some shortcutsthat you can take depending on what youre most interested inreading about. If you are interested solely in the DBI, you can skip is acomparison between the DBI and ODBC and is mainly of interest todatabase geeks, design aficionados, and those people who haveWin32::ODBC
applications and are desperatelytrying to port them to DBI.
Heres a rundown of the book, chapter by chapter:
This introduction sets up the general feel for the book.
This chapter covers the basics of storing and retrieving data eitherwith core Perl functions through the use ofdelimited or fixed-width flat-file databases, or via non-DBI modules such asAnyDBM_File
, Storable
,Data::Dumper
and friends. Although the DBIisnt used in this chapter, the way theStorable
and Data::Dumper
modules are used to pack Perl data structures into strings can easilybe applied to the DBI.
This chapter is a basic overview of SQL and relational databases andhow you can write simple but powerful SQL statements to query andmanipulate your database. If you already know some SQL, you can skipthis chapter. If you dont know SQL, we advise you to read thischapter since the later chapters assume you have a basic knowledge ofSQL and relational databases.
This chapter introduces the DBI to you by discussing the architectureof the DBI and basic DBI operations such as connecting to databasesand handling errors. This chapter is essential reading and describesthe framework that the DBI provides to let you write simple,powerful, and robust programs.
This chapter is the meat of the DBI topic and discusses manipulatingthe data within your databasethat is, retrieving data alreadystored in your database, inserting new data, and deleting andupdating existing data. We discuss the various ways in which you canperform these operations from the simple get it workingstage to more advanced and optimized techniques for manipulatingdata.
This chapter covers more advanced topics within the sphere of the DBIsuch as specifying attributes to fine-tune the operation of DBIwithin your applications, working with LONG/LOB datatypes, statementand database metadata, and finally transaction handling.
This chapter discusses the differences in design between DBI andODBC, the other portable database API. And, of course, this chapterhighlights why DBI is easier to program with.
This chapter covers two topics that arent exactly part of thecore DBI, per se , but are extremely useful toknow about. First, we discuss the DBI shell, a command-line tool thatallows you to connect to databases and issue arbitrary queries.Second, we discuss the proxy architecture that the DBI can use,which, among other things, allows you to connect scripts on onemachine to databases on another machine without needing to installany database networking software. For example, you can connect ascript running on a Unix box to a Microsoft Access database runningon a Microsoft Windows box.
This appendix contains the DBI specification, which is distributedwith DBI.pm
.
This appendix contains useful extra information on each of thecommonly used DBDs and their corresponding databases.
This appendix contains the charter for the Ancient Sacred LandscapeNetwork, which focuses on preserving sites such as the megalithicsites used for examples in this book.
Resources
To help you navigate some of the topicsin this book, here are some resources that you might want to checkout before, during, and after reading this book:
http://dbi.perl.org
The DBI home page. This site contains lots of useful informationabout DBI and where to get the various modules from. It also haslinks to the very active dbi-users mailing list and archives.
http://www.perl.com/CPAN
This site includes the Comprehensive Perl Archive Networkmultiplexer, upon which you find a whole host of useful modulesincluding the DBI.
An Introduction to Database Systems, by C. J. Date
This book is the standard textbook on database systems and is highlyrecommended reading.
A Guide to the SQL Standard, by C. J. Date and Hugh Darwen
An excellent book thats detailed but small and very readable.
http://w3.one.net/~jhoffman/sqltut.htm
http://www.jcc.com/SQLPages/jccs_sql.htm
http://www.contrib.andrew.cmu.edu/~shadow/sql.html
These web sites contain information, specifications, and links on theSQL query language, of which we present a primer in . Further information can be found by enteringSQL tutorial or similar expressions into your favoriteweb search engine.
Learning Perl, by Randal Schwartz and Tom Christiansen
A hands-on tutorial designed to get you writing useful Perl scriptsas quickly as possible. Exercises (with complete solutions) accompanyeach chapter. A lengthy new chapter introduces you to CGIprogramming, while touching also on the use of library modules,references, and Perls object-oriented constructs.
Programming Perl, by Larry Wall, Tom Christiansen, and Randal Schwartz
The authoritative guide to Perl version 5, the scripting utility thathas established itself as the programming tool of choice for theWorld Wide Web, Unix system administration, and a vast range of otherapplications. Version 5 of Perl includes object-oriented programmingfacilities. The book is coauthored by Larry Wall, the creator ofPerl.