Upload
connor-jefferson
View
46
Download
0
Embed Size (px)
DESCRIPTION
February 5 th , 2007. DBI tutorial. What is DBI. DBI is short for Database Interface, which is a Perl module. Relational Database. Relational database is a bunch of tables, each row of which is a record A record contains pieces of information called fields A table looks like:. - PowerPoint PPT Presentation
Citation preview
DBI tutorial
February 5th, 2007
What is DBI
DBI is short for Database DBI is short for Database Interface, which is a Perl moduleInterface, which is a Perl module
Relational Database
Relational database is a bunch of Relational database is a bunch of tables, each row of which is a tables, each row of which is a recordrecord
A record contains pieces of A record contains pieces of information called fieldsinformation called fields
A table looks like:A table looks like:
An Example of Table
34424994177Jacky4
34424954175Jamie6
34424974173Yeming8
PhoneTitlenameid
SQL
SQL is short for Structured Query SQL is short for Structured Query Language, which is invented at Language, which is invented at IBM at 1970’sIBM at 1970’s
Four main commands: SELECT, Four main commands: SELECT, INSERT, DELETE, UPDATEINSERT, DELETE, UPDATE
Before manipulating a table, use Before manipulating a table, use CREATE TABLE to create a new CREATE TABLE to create a new tabletable
You can also DROP a tableYou can also DROP a table
SQL Examples
SELECT * FROM employees WHERE SELECT * FROM employees WHERE name = ‘jacky' name = ‘jacky'
DELETE FROM employees WHERE id = 8 DELETE FROM employees WHERE id = 8 UPDATE employees SET phone = UPDATE employees SET phone =
phone+1 WHERE id = 8 phone+1 WHERE id = 8 INSERT INTO employees VALUES (10, INSERT INTO employees VALUES (10,
‘joyce', ‘5678’, 3445678 )‘joyce', ‘5678’, 3445678 )
What DBI Is for?
Every database system is a little differentEvery database system is a little different Every database system has a library, Every database system has a library,
which is different from each otherwhich is different from each other A extra layer is added to solve the A extra layer is added to solve the
difference among different database difference among different database systemssystems
That's what Perl's DBI module is all aboutThat's what Perl's DBI module is all about Using DBI, the programmer doesn’t need Using DBI, the programmer doesn’t need
to know the details of different databases to know the details of different databases systemssystems
How DBI works?
DBI talks Database Driver (DBD), DBI talks Database Driver (DBD), which has the library for the real which has the library for the real database system and knows how database system and knows how to talk to the database system to talk to the database system directlydirectly
How to Use DBI?
DBI main functions:DBI main functions: connect; connect; prepare; prepare; disconnect; disconnect; do; do; execute; execute; fetchrow_array; fetchrow_array; rows; rows;
Connect
driver source: driver source: dbi:mysql:host=torch.cs.dal.ca;datdbi:mysql:host=torch.cs.dal.ca;database=torchacount;port=3306abase=torchacount;port=3306
username: torch accountusername: torch account password: Banner IDpassword: Banner ID
Code Snippet
(1) use DBI;(1) use DBI; (2) my $dbh = DBI -> connect (2) my $dbh = DBI -> connect
('dbi:mysql:host=torch;database=yeming;port=3306','yeming','B0('dbi:mysql:host=torch;database=yeming;port=3306','yeming','B0xxxxxxxxxxxx') ||die ') ||die "Database (3) connection not made: $DBI::errstr";"Database (3) connection not made: $DBI::errstr";
(4) $dbh->do("DROP TABLE employees");(4) $dbh->do("DROP TABLE employees"); (5) $dbh->do("CREATE TABLE employees ( id INTEGER NOT NULL, (5) $dbh->do("CREATE TABLE employees ( id INTEGER NOT NULL, name VARCHAR(128), name VARCHAR(128), title VARCHAR(128), title VARCHAR(128), phone CHAR(8) phone CHAR(8) )");)");
(6) $dbh->do("INSERT INTO employees VALUES('8', 'yeming', '4173', (6) $dbh->do("INSERT INTO employees VALUES('8', 'yeming', '4173', '3442497')");'3442497')");
(7) $dbh->do("INSERT INTO employees VALUES('6', 'jamie', '4173', (7) $dbh->do("INSERT INTO employees VALUES('6', 'jamie', '4173', '3442495')");'3442495')");
(8) $dbh->do("INSERT INTO employees VALUES('4', 'jacky', '4445', (8) $dbh->do("INSERT INTO employees VALUES('4', 'jacky', '4445', '3442493')"); '3442493')");
Code Snippet
(9) my $sth = $dbh->prepare('SELECT * FROM employees WHERE name=?');(9) my $sth = $dbh->prepare('SELECT * FROM employees WHERE name=?');
(10) print "Enter name>";(10) print "Enter name>"; (11) my $name=<>;(11) my $name=<>; (12) my @data;(12) my @data; (13) chomp $name;(13) chomp $name; (14) $sth->execute($name) or die "Couldn't execute statement". $sth->errstr;(14) $sth->execute($name) or die "Couldn't execute statement". $sth->errstr; (15) while(@data=$sth->fetchrow_array())(15) while(@data=$sth->fetchrow_array()) {{ (16) my $id=$data[0];(16) my $id=$data[0]; (17) my $name=$data[1];(17) my $name=$data[1]; (18) my $title=$data[2];(18) my $title=$data[2]; (19) my $phone=$data[3];(19) my $phone=$data[3]; (20) print " $id $name $title $phone \n"(20) print " $id $name $title $phone \n" }}
(21) $dbh->do("DELETE FROM employees WHERE name='jacky'");(21) $dbh->do("DELETE FROM employees WHERE name='jacky'"); (22) $sth->execute('jacky') || die "Couldn't execute statement". $sth->errstr;(22) $sth->execute('jacky') || die "Couldn't execute statement". $sth->errstr; (23) my $rows= $sth->rows;(23) my $rows= $sth->rows; (24) print "There is $rows records about jacky\n ";(24) print "There is $rows records about jacky\n ";
Code Snippet
(25) $dbh->do("UPDATE employees SET phone=4021828 (25) $dbh->do("UPDATE employees SET phone=4021828 WHERE name='yeming'");WHERE name='yeming'");
(26) $name='yeming';(26) $name='yeming'; (27) $sth->execute($name) or die "Couldn't execute (27) $sth->execute($name) or die "Couldn't execute
statement". $sth->errstr;statement". $sth->errstr; (28) while(@data=$sth->fetchrow_array())(28) while(@data=$sth->fetchrow_array()) {{ (29) my $id=$data[0];(29) my $id=$data[0]; (30)my $name=$data[1];(30)my $name=$data[1]; (31) my $title=$data[2];(31) my $title=$data[2]; (32) my $phone=$data[3];(32) my $phone=$data[3]; (33) print " $id $name $title $phone \n"(33) print " $id $name $title $phone \n" }} (34) $dbh->disconnect(); (34) $dbh->disconnect();
Sources
http://www.perl.com/pub/a/http://www.perl.com/pub/a/1999/10/DBI.html1999/10/DBI.html
http://www.pantz.org/database/http://www.pantz.org/database/mysql/mysqlcommands.shtmlmysql/mysqlcommands.shtmlhttp://www.unix.org.ua/orelly/perl/perlnut/ch12_03.htm
Questions?Questions?