SlideShare a Scribd company logo
1 of 24
DBI
SQL dialects
SQLite
MySQL
PostgreSQL
Oracle
Case insensitive
SQL language is case insensitive
Textual data is case sensitive
SQL reserved words in CAPs
– By convention
– Not mandatory
SQLite
$ sqlite3 address.db
SQL Create
CREATE TABLE address (
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
city TEXT,
state TEXT
);
SQLite
sqlite> .tables
sqlite> .schema address
sqlite> .quit
SQL Insert
INSERT INTO address
(first_name, last_name, city, state)
VALUES ('John', 'Doe', 'Hollywood', 'CA');
SQL Update
UPDATE address
SET city='Hollywood',
state='FL'
WHERE id=1;
SQL Select
SELECT * FROM address;
-- show everything
SELECT *
FROM address
WHERE state='FL';
-- only florida
SELECT first_name, last_name
FROM address
WHERE state='FL';
-- specific fields
SQL Delete
DELETE FROM address;
-- very dangerous
-- deletes everything
DELETE FROM address
WHERE state='FL';
-- use a WHERE condition
DBI
$ cpanm DBD::SQLite
DBI->connect()
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
my $dbh = DBI->connect(
"dbi:SQLite:dbname=address.db", "", ""
);
prepare/execute/fetch
$sth = $dbh->prepare ("
SELECT id, first_name, last_name, city, state
FROM address
WHERE city=?
AND state=?
");
$sth->execute ('Sacramento', 'CA');
# fills in values to the ? placeholders
while (my $row = $sth->fetch()) {
my ($id, $first_name, $last_name, $city, $state) = @$row;
print "$city, $state: $first_name $last_namen";
}
Placeholder
?
Security: The placeholder escapes data
– Counters SQL injection attack
Optimization: Prepare once, execute many. Sometimes faster.
– MySQL: no difference
– Oracle: faster
fetchrow_hashref
while (my $row = $sth->fetchrow_hashref() ) {
print Dumper $row;
}
# $VAR1 = {
# 'city' => 'Sacramento',
# 'id' => 2,
# 'state' => 'CA',
# 'first_name' => 'Jane',
# 'last_name' => 'Doe'
# };
fetchall_arrayref()
# No WHERE condition. Get everything.
$sth = $dbh->prepare ("
SELECT id, first_name, last_name, city, state
FROM address
");
$sth->execute ();
my $rows = $sth->fetchall_arrayref();
foreach my $row (@$rows) {
my ($id, $first_name, $last_name, $city, $state) = @$row;
print "$city, $state: $first_name $last_namen";
}
No fetch
$sth = $dbh->prepare ("
INSERT INTO address (first_name, last_name, city, state)
VALUES (?,?,?,?)
");
$sth->execute('Doug', 'Engelbart', 'Moscow', 'Idaho');
No fetch
$sth = $dbh->prepare ("
UPDATE address SET state=?
WHERE city=?
");
$sth->execute ('TN', 'Moscow');
No fetch
$sth = $dbh->prepare ("
UPDATE address SET first_name=?, last_name=?
WHERE city=? AND state=?
");
$sth->execute ('Edgar', 'Codd', 'TN', 'Moscow');
No fetch
$sth = $dbh->prepare ("
DELETE FROM address
WHERE city=? AND state=?
");
$sth->execute ('Moscow', 'TN');
last_insert_id()
# after your INSERT statement
my $row_id = $dbh->last_insert_id(
undef, undef, undef, undef
);
print "row_id: $row_idn";
Caching the dbh
Opening a database connection incurs overhead.
You want to cache the database handle
– Open once.
– Keep it open.
Example of caching on next slide
state
use feature 'state'; # perl 5.10
sub get_dbh {
state $dbh;
if (!$dbh) {
$dbh = DBI->connect(
"dbi:SQLite:dbname=address.db", "", ""
);
}
return $dbh;
}
DBI

More Related Content

What's hot

AKA BRANDBOOK 2016 NEW small
AKA BRANDBOOK 2016 NEW smallAKA BRANDBOOK 2016 NEW small
AKA BRANDBOOK 2016 NEW small
Rob Jones FCCA
 
Perspec sys tokenization paper_a4
Perspec sys tokenization paper_a4Perspec sys tokenization paper_a4
Perspec sys tokenization paper_a4
Mullrich1012
 
Tips for using Firebird system tables
Tips for using Firebird system tablesTips for using Firebird system tables
Tips for using Firebird system tables
Mind The Firebird
 
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Hiroshi Shibamura
 

What's hot (20)

Building Your First Widget
Building Your First WidgetBuilding Your First Widget
Building Your First Widget
 
Php
PhpPhp
Php
 
AKA BRANDBOOK 2016 NEW small
AKA BRANDBOOK 2016 NEW smallAKA BRANDBOOK 2016 NEW small
AKA BRANDBOOK 2016 NEW small
 
Migrare da symfony 1 a Symfony2
 Migrare da symfony 1 a Symfony2  Migrare da symfony 1 a Symfony2
Migrare da symfony 1 a Symfony2
 
[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for Developer[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for Developer
 
How to work with legacy code PHPers Rzeszow #2
How to work with legacy code PHPers Rzeszow #2How to work with legacy code PHPers Rzeszow #2
How to work with legacy code PHPers Rzeszow #2
 
How to work with legacy code
How to work with legacy codeHow to work with legacy code
How to work with legacy code
 
wget.pl
wget.plwget.pl
wget.pl
 
Facebook
FacebookFacebook
Facebook
 
Sugar introduction
Sugar introductionSugar introduction
Sugar introduction
 
6. hello popescu 2
6. hello popescu 26. hello popescu 2
6. hello popescu 2
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Perspec sys tokenization paper_a4
Perspec sys tokenization paper_a4Perspec sys tokenization paper_a4
Perspec sys tokenization paper_a4
 
WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHP
 
FormValidator::LazyWay で検証ルールをまとめよう
FormValidator::LazyWay で検証ルールをまとめようFormValidator::LazyWay で検証ルールをまとめよう
FormValidator::LazyWay で検証ルールをまとめよう
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
 
Tips for using Firebird system tables
Tips for using Firebird system tablesTips for using Firebird system tables
Tips for using Firebird system tables
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016
 
Practica csv
Practica csvPractica csv
Practica csv
 
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
 

Similar to DBI

Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Masahiro Nagano
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
jhchabran
 
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 TokyoIntroduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
Masahiro Nagano
 
Hi,you covered mostly things.there are issue to point and link poi.pdf
Hi,you covered mostly things.there are issue to point and link poi.pdfHi,you covered mostly things.there are issue to point and link poi.pdf
Hi,you covered mostly things.there are issue to point and link poi.pdf
aryan9007
 

Similar to DBI (20)

Php.docx
Php.docxPhp.docx
Php.docx
 
Bouncingballs sh
Bouncingballs shBouncingballs sh
Bouncingballs sh
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order Functions
 
Php functions
Php functionsPhp functions
Php functions
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
Presentation1
Presentation1Presentation1
Presentation1
 
Pop3ck sh
Pop3ck shPop3ck sh
Pop3ck sh
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
DBI
DBIDBI
DBI
 
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 TokyoIntroduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
 
Quill + Spark = Better Together
Quill + Spark = Better TogetherQuill + Spark = Better Together
Quill + Spark = Better Together
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding Horrors
 
Php My Sql
Php My SqlPhp My Sql
Php My Sql
 
Hi,you covered mostly things.there are issue to point and link poi.pdf
Hi,you covered mostly things.there are issue to point and link poi.pdfHi,you covered mostly things.there are issue to point and link poi.pdf
Hi,you covered mostly things.there are issue to point and link poi.pdf
 
Everything About PowerShell
Everything About PowerShellEverything About PowerShell
Everything About PowerShell
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
 
Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)
 
Php 101: PDO
Php 101: PDOPhp 101: PDO
Php 101: PDO
 

More from Lambert Lum (6)

Database Theory
Database TheoryDatabase Theory
Database Theory
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Web Crawlers in Perl
Web Crawlers in PerlWeb Crawlers in Perl
Web Crawlers in Perl
 
Moose: Perl Objects
Moose: Perl ObjectsMoose: Perl Objects
Moose: Perl Objects
 
Pack/Unpack: manipulate binary data
Pack/Unpack: manipulate binary dataPack/Unpack: manipulate binary data
Pack/Unpack: manipulate binary data
 

Recently uploaded

怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
vexqp
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
vexqp
 
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
vexqp
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
gajnagarg
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
vexqp
 
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
Health
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Klinik kandungan
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schs
cnajjemba
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
nirzagarg
 
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
wsppdmt
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
gajnagarg
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
ahmedjiabur940
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
q6pzkpark
 

Recently uploaded (20)

5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
 
Harnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxHarnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptx
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
 
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
 
Data Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdfData Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdf
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
 
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schs
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
 
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
SR-101-01012024-EN.docx  Federal Constitution  of the Swiss ConfederationSR-101-01012024-EN.docx  Federal Constitution  of the Swiss Confederation
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
 

DBI

  • 1. DBI
  • 3. Case insensitive SQL language is case insensitive Textual data is case sensitive SQL reserved words in CAPs – By convention – Not mandatory
  • 5. SQL Create CREATE TABLE address ( id INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT, city TEXT, state TEXT );
  • 7. SQL Insert INSERT INTO address (first_name, last_name, city, state) VALUES ('John', 'Doe', 'Hollywood', 'CA');
  • 8. SQL Update UPDATE address SET city='Hollywood', state='FL' WHERE id=1;
  • 9. SQL Select SELECT * FROM address; -- show everything SELECT * FROM address WHERE state='FL'; -- only florida SELECT first_name, last_name FROM address WHERE state='FL'; -- specific fields
  • 10. SQL Delete DELETE FROM address; -- very dangerous -- deletes everything DELETE FROM address WHERE state='FL'; -- use a WHERE condition
  • 12. DBI->connect() #!/usr/bin/perl use warnings; use strict; use DBI; my $dbh = DBI->connect( "dbi:SQLite:dbname=address.db", "", "" );
  • 13. prepare/execute/fetch $sth = $dbh->prepare (" SELECT id, first_name, last_name, city, state FROM address WHERE city=? AND state=? "); $sth->execute ('Sacramento', 'CA'); # fills in values to the ? placeholders while (my $row = $sth->fetch()) { my ($id, $first_name, $last_name, $city, $state) = @$row; print "$city, $state: $first_name $last_namen"; }
  • 14. Placeholder ? Security: The placeholder escapes data – Counters SQL injection attack Optimization: Prepare once, execute many. Sometimes faster. – MySQL: no difference – Oracle: faster
  • 15. fetchrow_hashref while (my $row = $sth->fetchrow_hashref() ) { print Dumper $row; } # $VAR1 = { # 'city' => 'Sacramento', # 'id' => 2, # 'state' => 'CA', # 'first_name' => 'Jane', # 'last_name' => 'Doe' # };
  • 16. fetchall_arrayref() # No WHERE condition. Get everything. $sth = $dbh->prepare (" SELECT id, first_name, last_name, city, state FROM address "); $sth->execute (); my $rows = $sth->fetchall_arrayref(); foreach my $row (@$rows) { my ($id, $first_name, $last_name, $city, $state) = @$row; print "$city, $state: $first_name $last_namen"; }
  • 17. No fetch $sth = $dbh->prepare (" INSERT INTO address (first_name, last_name, city, state) VALUES (?,?,?,?) "); $sth->execute('Doug', 'Engelbart', 'Moscow', 'Idaho');
  • 18. No fetch $sth = $dbh->prepare (" UPDATE address SET state=? WHERE city=? "); $sth->execute ('TN', 'Moscow');
  • 19. No fetch $sth = $dbh->prepare (" UPDATE address SET first_name=?, last_name=? WHERE city=? AND state=? "); $sth->execute ('Edgar', 'Codd', 'TN', 'Moscow');
  • 20. No fetch $sth = $dbh->prepare (" DELETE FROM address WHERE city=? AND state=? "); $sth->execute ('Moscow', 'TN');
  • 21. last_insert_id() # after your INSERT statement my $row_id = $dbh->last_insert_id( undef, undef, undef, undef ); print "row_id: $row_idn";
  • 22. Caching the dbh Opening a database connection incurs overhead. You want to cache the database handle – Open once. – Keep it open. Example of caching on next slide
  • 23. state use feature 'state'; # perl 5.10 sub get_dbh { state $dbh; if (!$dbh) { $dbh = DBI->connect( "dbi:SQLite:dbname=address.db", "", "" ); } return $dbh; }