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

Building Your First Widget
Building Your First WidgetBuilding Your First Widget
Building Your First WidgetChris Wilcoxson
 
AKA BRANDBOOK 2016 NEW small
AKA BRANDBOOK 2016 NEW smallAKA BRANDBOOK 2016 NEW small
AKA BRANDBOOK 2016 NEW smallRob Jones FCCA
 
[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for Developer[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for DeveloperNHN FORWARD
 
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 #2Michał Kruczek
 
How to work with legacy code
How to work with legacy codeHow to work with legacy code
How to work with legacy codeMichał Kruczek
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency InjectionRifat Nabi
 
Perspec sys tokenization paper_a4
Perspec sys tokenization paper_a4Perspec sys tokenization paper_a4
Perspec sys tokenization paper_a4Mullrich1012
 
WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPandrewnacin
 
FormValidator::LazyWay で検証ルールをまとめよう
FormValidator::LazyWay で検証ルールをまとめようFormValidator::LazyWay で検証ルールをまとめよう
FormValidator::LazyWay で検証ルールをまとめようDaisuke Komatsu
 
Tips for using Firebird system tables
Tips for using Firebird system tablesTips for using Firebird system tables
Tips for using Firebird system tablesMind The Firebird
 
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 2016Colin O'Dell
 
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 #2Hiroshi 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

Bouncingballs sh
Bouncingballs shBouncingballs sh
Bouncingballs shBen Pope
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistenceHugo Hamon
 
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 2011Masahiro Nagano
 
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 FunctionsDavid Golden
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB jhchabran
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data ObjectsWez Furlong
 
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 TokyoMasahiro Nagano
 
Quill + Spark = Better Together
Quill + Spark = Better TogetherQuill + Spark = Better Together
Quill + Spark = Better TogetherAlexander Ioffe
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding HorrorsMark Baker
 
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.pdfaryan9007
 
Everything About PowerShell
Everything About PowerShellEverything About PowerShell
Everything About PowerShellGaetano Causio
 
Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)Jeff Eaton
 

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

Software Testing
Software TestingSoftware Testing
Software TestingLambert Lum
 
Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionLambert Lum
 
Web Crawlers in Perl
Web Crawlers in PerlWeb Crawlers in Perl
Web Crawlers in PerlLambert Lum
 
Moose: Perl Objects
Moose: Perl ObjectsMoose: Perl Objects
Moose: Perl ObjectsLambert Lum
 
Pack/Unpack: manipulate binary data
Pack/Unpack: manipulate binary dataPack/Unpack: manipulate binary data
Pack/Unpack: manipulate binary dataLambert Lum
 

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

Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...shivangimorya083
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationBoston Institute of Analytics
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 

Recently uploaded (20)

Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project Presentation
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 

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