DBI: The Neophyte's Guide1 What is DBI? DBI = DataBase Interface DBI is database-independent DBI allows you to write code that interacts with databases.

Slides:



Advertisements
Similar presentations
Connecting to Databases. connecting to DB DB server typically a standalone application Server runs on localhost for smaller sites –i.e. Same machine as.
Advertisements

Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
W EB A PPLICATION D EVELOPMENT A PPLICATION T O B IO -I NFORMATICS -III Vicky Khanna M-Tech Bioinformatics.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Databases and Perl Using Perl to talk to databases.
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
CSE 190: Internet E-Commerce Lecture 13: Database code.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Perl DBI Scripting with the ILS Roy Zimmer Western Michigan University.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
SQL Injection Attacks CS 183 : Hypermedia and the Web UC Santa Cruz.
Introducing PHP Data Objects Wez Furlong
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Database Access Through the Web.
27/07/2001 Copyright 2001, Munica Corporation, 1 How to write a DBD driver Daini Xie Strathy President Munica Corporation
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
DBI tutorial February 5 th, What is DBI DBI is short for Database Interface, which is a Perl module DBI is short for Database Interface, which is.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
Dr Gordon Russell, Napier University Unit Embedded SQL - V3.0 1 Embedded SQL Unit 5.1.
Templates, Databases and Frameworks. Databases: DBI Common database interface for perl Provides a functional,
Advanced SQL: Cursors & Stored Procedures
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
Databases On The Web with perl Archie Warnock
3-Tier Client/Server Internet Example. TIER 1 - User interface and navigation Labeled Tier 1 in the following graphic, this layer comprises the entire.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Efficient RDF Storage and Retrieval in Jena2 Written by: Kevin Wilkinson, Craig Sayers, Harumi Kuno, Dave Reynolds Presented by: Umer Fareed 파리드.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class Four Instructor:
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class Three Instructor:
Database Access Using JDBC BCIS 3680 Enterprise Programming.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
SQL. What is a database? G a collection of data G Usually consists of entities and relations G An entity is an individual “object” that exists and is.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
MYSQL AND MYSQL WORKBENCH MIS2502 Data Analytics.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
3 Copyright © 2006, Oracle. All rights reserved. Designing and Developing for Performance.
COMP234 - perl Perl DBI Topics Database vs access methods DBMS and DBMS API's Relational database SEQUEL Perl DBI SQL.
Please remember to thank our sponsors!
Web Database Programming Using PHP
ASP.NET Programming with C# and SQL Server First Edition
Web Database Programming Using PHP
mysql and mysql workbench
Pengantar Keamanan Informasi
Error Handling Summary of the next few pages: Error Handling Cursors.
Perl Database – Just Enough
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Developing a Model-View-Controller Component for Joomla Part 3
Unit I-2.
Chapter 10 ADO.
Presentation transcript:

DBI: The Neophyte's Guide1 What is DBI? DBI = DataBase Interface DBI is database-independent DBI allows you to write code that interacts with databases independent of the underlying database A DBI program will work with little, or no, modification on Oracle, Informix, MySQL and so on...

DBI: The Neophyte's Guide2 What are DBDs? DBD = DataBase Driver Drivers are used by DBI to perform the actual database work Drivers are database-dependent Drivers exist for most popular databases including Oracle, Informix, Ingres, mSQL, MySQL, Solid and many more

DBI: The Neophyte's Guide3 Why do we need the DBI? Abstracted interface hides database-specific functionality from the programmer DBI scripts are generally completely portable between databases with little modification Faster application development and debugging cycle

DBI: The Neophyte's Guide4 What about ODBC? ODBC and DBI share similar goals and heritage: platform- and database-independence DBI is far more compact and less complex than ODBC. ODBC has a richer feature set Although ODBC drivers exist for many operating systems, they can be expensive and difficult to use. DBI is free and easy to configure and use A driver called DBD::ODBC exists and can be used to connect to ODBC-based databases such as Microsoft Access

DBI: The Neophyte's Guide5 DBI Resources The DBI home page – ``Programming the Perl DBI’’ -- Alligator Descartes and Tim Bunce. Can be ordered via amazon.com from the DBI home page This presentation and demonstration source code can be downloaded from the DBI home page

DBI: The Neophyte's Guide6 DBI Architecture Architectural Overview A Handy Reminder Handles –Driver Handles –Database Handles –Statement Handles Basic Database Operations –Connecting to the Database Data Sources –Disconnecting from the Database

DBI: The Neophyte's Guide7 Architectural Overview

DBI: The Neophyte's Guide8 A Handy Reminder A tip to help you remember which modules do what in DBI –DBI is DataBase Independent –DBD is DataBase Dependent

DBI: The Neophyte's Guide9 Handles All operations in DBI are performed via –DBI module as a static method –Driver Handles –Database Handle instance method –Statement Handle instance method Driver Handles are not used explicitly by the programmer in scripts

DBI: The Neophyte's Guide10 Driver Handles Driver Handles encapsulate a DBD There exists exactly one driver handle for each loaded DBD For example, a script using both MySQL and Oracle will have two instantiated driver handles Driver handles are never used directly in programs and are for internal use only by DBI

DBI: The Neophyte's Guide11 Database Handles Database Handles encapsulate a single connection to a database Database handles are created via a driver handle for the desired database type. This is done via the DBI->connect() method $dbh = DBI->connect( … );

DBI: The Neophyte's Guide12 Statement Handles Statement Handles encapsulate a statement to be issued to the database Statement handles are created via a database handle. That is, a statement is issued to the database represented by the database handle $sth = $dbh->prepare( “SELECT yadda FROM blah” ); Statement handles also allow you to fetch data from the issued statement from the database

DBI: The Neophyte's Guide13 Handle Naming Conventions Throughout the example code in this presentation and other DBI texts, you might see the following naming convention of variables –$drh = Driver Handle –$dbh = Database Handle –$sth = Statement Handle

DBI: The Neophyte's Guide14 Starting Out with DBI Initializing the DBI Data Sources Checking Available Drivers Connecting to the Database Disconnecting from the Database Example

DBI: The Neophyte's Guide15 Initializing the DBI DBI can be initialized very simply by adding use DBI; to your scripts DBI internally handles all driver loading automatically and DBDs should not normally be explicitly use ’d or require ’d in your scripts

DBI: The Neophyte's Guide16 Data Sources In summary, Data Source specification is about the most non-portable aspect of DBI In reality, a well-written DBI script that uses standard SQL can be ported by simply changing the DSN in the DBI->connect() call

DBI: The Neophyte's Guide17 Connecting to the Database The very first database operation you must do! Use the DBI->connect() method For example $dbh = DBI->connect( ‘dbi:Oracle:DEV’, ‘user’, ‘pass’ ); That is, we minimally specify the Data Source Name of the database, a username and password If successful, this will return a valid database handle

DBI: The Neophyte's Guide18 Disconnecting from the Database Once all work has been completed, you must disconnect from the database Frees any used database and system resources Achieved by calling disconnect () against a valid database handle. For example: $dbh->disconnect();

DBI: The Neophyte's Guide19 Example This example connects to a single Oracle database called DEV #!/usr/bin/perl -w ### Load the DBI module use DBI; ### Perform the connection using the Oracle driver $dbh = DBI->connect( "dbi:Oracle:DEV", "username", "password" ) or die "Can't connect to Oracle database: $DBI::errstr\n"; ### Disconnect from the database $dbh->disconnect(); exit;

DBI: The Neophyte's Guide20 Example This example demonstrates connecting to two different databases of different types #!/usr/bin/perl -w ### Load the DBI module use DBI; ### Perform the connection using the Oracle driver $dbh1 = DBI->connect( "dbi:Oracle:DEV", "username", "password" ) or die "Can't connect to Oracle database: $DBI::errstr\n"; $dbh2 = DBI->connect( "dbi:mSQL:host:dbname:1114", "username", "password" ) or die "Can't connect to mSQL database: $DBI::errstr\n"; $dbh1->disconnect(); $dbh2->disconnect(); exit;

DBI: The Neophyte's Guide21 Interacting with the Database Simple Queries –Preparing Statements –Executing Statements –Fetching Data Non- SELECT Statements Bind Values Optimizing $dbh->do()

DBI: The Neophyte's Guide22 Simple Queries The process of retrieving data from the database is a 4-stage cycle –Preparing the statement –Executing the statement –Fetching the data –Finishing the statement

DBI: The Neophyte's Guide23 Preparing the Statement The $dbh->prepare( $statement ) method is used This can mean different things depending on the underlying DBD used. Oracle will use this stage to send the statement to the database for parsing. However, mSQL does nothing here at all The SQL statement is simply a Perl string and therefore can be supplied as a literal string or as a variable holding a built on-the-fly statement

DBI: The Neophyte's Guide24 Preparing the Statement ( cont. ) #!/usr/bin/perl -w ### Load the DBI module use DBI; ### The database handle $dbh = DBI->connect( "dbi:Oracle:DEV", "username", "password" ); ### Prepare the statement handle $sth = $dbh->prepare( "SELECT id, name FROM megaliths" );... exit;

DBI: The Neophyte's Guide25 Executing the Statement Once the statement has been successfully prepared, it must be executed Depending on the database, this stage typically executes the prepared statement within the database and generates a result set ready for fetching Simply a case of calling $sth->execute() against your prepared statement handle

DBI: The Neophyte's Guide26 Fetching the Data The fetch stage retrieves the data from the database row-by-row by use of a cursor The most commonly used fetch style is to retrieve the data as a Perl list = $sth->fetchrow_array() You should continuously loop calling fetchrow_array() until all rows are returned

DBI: The Neophyte's Guide27 Fetching Data - Example #/usr/bin/perl -w ### Load the DBI module use DBI; ### Connect to the database $dbh = DBI->connect( ‘dbi:Oracle:DEV’, ‘username’, ‘password’ ); ### Prepare and execute the statement $sth = $dbh->prepare( “SELECT name, type FROM megaliths” ); $sth->execute(); ### Fetch the data while = $sth->fetchrow_array() ) { print "Megalith site $row[0] is a $row[1]\n"; } ### Disconnect from the database $dbh->disconnect(); exit;

DBI: The Neophyte's Guide28 Fetching the Data There are additional methods for fetching data from the statement handle: fetchrow_arrayref() fetchrow_hashref()

DBI: The Neophyte's Guide29 Finishing the Statement DBI automatically marks the statement handles as inactive once all the data has been fetched from it You can manually mark statement handles as being inactive via the $sth->finish() method, but you should usually not need to invoke this method in your scripts

DBI: The Neophyte's Guide30 Non- SELECT Statements Statements other than queries can be issued to the database via DBI For example, UPDATE, DELETE and INSERT statements These statements are issued by calling $dbh->do() which combines preparation and execution in a single step

DBI: The Neophyte's Guide31 Non- SELECT Statements ( cont. ) For example: $dbh->do( “DELETE FROM megaliths” ); $dbh->do( “DROP TABLE megaliths” ); $dbh->do( “INSERT INTO megaliths VALUES ( 1, ‘Stonehenge’, ‘Wiltshire’ )” );

DBI: The Neophyte's Guide32 Bind Values A bind value is a value that can be bound to a placeholder declared within a SQL statement This is similar to creating an on-the-fly SQL statement such as: $sth = $dbh->prepare( " SELECT name, location FROM megaliths WHERE name = ". $dbh->quote( $siteName ). " " );

DBI: The Neophyte's Guide33 Bind Values ( cont. ) However, instead of interpolating the generated value into the SQL statement, you specify a placeholder and then bind the generated value to that For example: $sth = $dbh->prepare( " SELECT name, location FROM megaliths WHERE name = ? " ); $sth->bind_param( 1, $dbh->quote( ‘Avebury’ ) );

DBI: The Neophyte's Guide34 Optimizing $dbh->do() The default implementation of $dbh->do() calls prepare(), execute() and finish() for each invocation of $dbh->do() This can be optimized for repeated iterations by hand-preparing the statement but repeatedly executing the same statement handle This is very efficient when used with bind values

DBI: The Neophyte's Guide35 Optimizing $dbh->do() ### Prepare the statement handle $sth = $dbh->prepare( “DELETE FROM megaliths WHERE id = ?” ); ### Remove the first 100 rows one-by-one... $loopCounter = 0; while ( $loopCounter < 100 ) { $sth->execute( $loopCounter ); $loopCounter++; } $sth->finish();... This is far faster than repeatedly preparing the same statement over and over again