Presentation is loading. Please wait.

Presentation is loading. Please wait.

DBI tutorial February 5 th, 2007. What is DBI DBI is short for Database Interface, which is a Perl module DBI is short for Database Interface, which is.

Similar presentations


Presentation on theme: "DBI tutorial February 5 th, 2007. What is DBI DBI is short for Database Interface, which is a Perl module DBI is short for Database Interface, which is."— Presentation transcript:

1 DBI tutorial February 5 th, 2007

2 What is DBI DBI is short for Database Interface, which is a Perl module DBI is short for Database Interface, which is a Perl module

3 Relational Database Relational database is a bunch of tables, each row of which is a record Relational database is a bunch of tables, each row of which is a record A record contains pieces of information called fields A record contains pieces of information called fields A table looks like: A table looks like:

4 An Example of Table 34424994177Jacky4 34424954175Jamie6 34424974173Yeming8 PhoneTitlenameid

5 SQL SQL is short for Structured Query Language, which is invented at IBM at 1970’s SQL is short for Structured Query Language, which is invented at IBM at 1970’s Four main commands: SELECT, INSERT, DELETE, UPDATE Four main commands: SELECT, INSERT, DELETE, UPDATE Before manipulating a table, use CREATE TABLE to create a new table Before manipulating a table, use CREATE TABLE to create a new table You can also DROP a table You can also DROP a table

6 SQL Examples SELECT * FROM employees WHERE name = ‘jacky' SELECT * FROM employees WHERE name = ‘jacky' DELETE FROM employees WHERE id = 8 DELETE FROM employees WHERE id = 8 UPDATE employees SET phone = phone+1 WHERE id = 8 UPDATE employees SET phone = phone+1 WHERE id = 8 INSERT INTO employees VALUES (10, ‘joyce', ‘5678’, 3445678 ) INSERT INTO employees VALUES (10, ‘joyce', ‘5678’, 3445678 )

7 What DBI Is for? Every database system is a little different Every database system is a little different Every database system has a library, which is different from each other Every database system has a library, which is different from each other A extra layer is added to solve the difference among different database systems A extra layer is added to solve the difference among different database systems That's what Perl's DBI module is all about That's what Perl's DBI module is all about Using DBI, the programmer doesn’t need to know the details of different databases systems Using DBI, the programmer doesn’t need to know the details of different databases systems

8 How DBI works? DBI talks Database Driver (DBD), which has the library for the real database system and knows how to talk to the database system directly DBI talks Database Driver (DBD), which has the library for the real database system and knows how to talk to the database system directly

9 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;

10 Connect driver source: dbi:mysql:host=torch.cs.dal.ca;database =torchacount;port=3306 driver source: dbi:mysql:host=torch.cs.dal.ca;database =torchacount;port=3306 username: torch account username: torch account password: Banner ID password: Banner ID

11 Code Snippet (1) use DBI; (1) use DBI; (2) my $dbh = DBI -> connect ('dbi:mysql:host=torch;database=yeming;port=3306','yeming','B0xxxxxx') ||die "Database (3) connection not made: $DBI::errstr"; (2) my $dbh = DBI -> connect ('dbi:mysql:host=torch;database=yeming;port=3306','yeming','B0xxxxxx') ||die "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', '3442497')"); (6) $dbh->do("INSERT INTO employees VALUES('8', 'yeming', '4173', '3442497')"); (7) $dbh->do("INSERT INTO employees VALUES('6', 'jamie', '4173', '3442495')"); (7) $dbh->do("INSERT INTO employees VALUES('6', 'jamie', '4173', '3442495')"); (8) $dbh->do("INSERT INTO employees VALUES('4', 'jacky', '4445', '3442493')"); (8) $dbh->do("INSERT INTO employees VALUES('4', 'jacky', '4445', '3442493')");

12 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 ";

13 Code Snippet (25) $dbh->do("UPDATE employees SET phone=4021828 WHERE name='yeming'"); (25) $dbh->do("UPDATE employees SET phone=4021828 WHERE name='yeming'"); (26) $name='yeming'; (26) $name='yeming'; (27) $sth->execute($name) or die "Couldn't execute statement". $sth- >errstr; (27) $sth->execute($name) or die "Couldn't execute 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();

14 Sources http://www.perl.com/pub/a/1999/10/D BI.html http://www.perl.com/pub/a/1999/10/D BI.html http://www.perl.com/pub/a/1999/10/D BI.html http://www.perl.com/pub/a/1999/10/D BI.html http://www.pantz.org/database/mysql/ mysqlcommands.shtml http://www.pantz.org/database/mysql/ mysqlcommands.shtml http://www.pantz.org/database/mysql/ mysqlcommands.shtml http://www.pantz.org/database/mysql/ mysqlcommands.shtml http://www.unix.org.ua/orelly/perl/perlnut/ch12_03.htm

15 Questions?


Download ppt "DBI tutorial February 5 th, 2007. What is DBI DBI is short for Database Interface, which is a Perl module DBI is short for Database Interface, which is."

Similar presentations


Ads by Google