SlideShare a Scribd company logo
http://www.tutorialspoint.com/sqlite/sqlite_perl.htm Copyright © tutorialspoint.com
SQLITE PERL TUTORIAL
Installation
The SQLite3 can be integrated with Perl using Perl DBI module which is a database access module
for the Perl programming language. It defines a set of methods, variables and conventions that
provide a standard database interface.
Here are simple steps to install DBI module on your Linux/Unix machine:
$ wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.625.tar.gz
$ tar xvfz DBI-1.625.tar.gz
$ cd DBI-1.625
$ perl Makefile.PL
$ make
$ make install
If you need to install SQLite driver for DBI, then it can be installed as follows:
$ wget http://search.cpan.org/CPAN/authors/id/M/MS/MSERGEANT/DBD-SQLite-1.11.tar.gz
$ tar xvfz DBD-SQLite-1.11.tar.gz
$ cd DBD-SQLite-1.11
$ perl Makefile.PL
$ make
$ make install
DBI Interface APIs
Following are important DBI routines which can sufice your requirement to work with SQLite
database from your Perl program. If you are looking for a more sophisticated application then you
can look into Perl DBI official documentation.
S.N. API & Description
1 DBI->connect($data_source, "", "", %attr)
Establishes a database connection, or session, to the requested $data_source. Returns
a database handle object if the connection succeeds.
Datasource has the form like : DBI:SQLite:dbname='test.db' SQLite is SQLite driver
name and test.db is the name of SQLite database file. If the filename is given as
':memory:', it will create an in-memory database in RAM that lasts only for the duration
of the session.
If filename is actual device file name, then it attempts to open the database file by
using its value. If no file by that name exists then a new database file by that name gets
created.
You keep second and third paramter as blank strings and last parameter is to pass
various attributes as shown below in the example.
2 $dbh->do($sql)
This routine prepares and executes a single SQL statement. Returns the number of rows
affected or undef on error. A return value of -1 means the number of rows is not known,
not applicable, or not available. Here $dbh is a handle returned by DBI->connect() call.
3 $dbh->prepare($sql)
This routine prepares a statement for later execution by the database engine and
returns a reference to a statement handle object.
4 $sth->execute()
This routine performs whatever processing is necessary to execute the prepared
statement. An undef is returned if an error occurs. A successful execute always returns
true regardless of the number of rows affected. Here $sth is a statement handle
returned by $dbh->prepare($sql) call.
5 $sth->fetchrow_array()
This routine fetches the next row of data and returns it as a list containing the field
values. Null fields are returned as undef values in the list.
6 $DBI::err
This is equivalent to $h->err, where $h is any of the handle types like $dbh, $sth, or $drh.
This returns native database engine error code from the last driver method called.
7 $DBI::errstr
This is equivalent to $h->errstr, where $h is any of the handle types like $dbh, $sth, or
$drh. This returns the native database engine error message from the last DBI method
called.
8 $dbh->disconnect()
This routine closes a database connection previously opened by a call to DBI-
>connect().
Connecting To Database
Following Perl code shows how to connect to an existing database. If database does not exist,
then it will be created and finally a database object will be returned.
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "SQLite";
my $database = "test.db";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfullyn";
Now let's run above program to create our database test.db in the current directory. You can
change your path as per your requirement. Keep above code in sqlite.pl file and execute it as
shown below. If database is successfully created then it will give following message:
$ chmod +x sqlite.pl
$ ./sqlite.pl
Open database successfully
Create a Table
Following Perl program will be used to create a table in previously created database:
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "SQLite";
my $database = "test.db";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfullyn";
my $stmt = qq(CREATE TABLE COMPANY
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL););
my $rv = $dbh->do($stmt);
if($rv < 0){
print $DBI::errstr;
} else {
print "Table created successfullyn";
}
$dbh->disconnect();
When above program is executed, it will create COMPANY table in your test.db and it will display
following messages:
Opened database successfully
Table created successfully
NOTE: in case you see following error in any of the operation:
DBD::SQLite::st execute failed: not an error(21) at dbdimp.c line 398
In this case you will have open dbdimp.c file available in DBD-SQLite installation and find out
sqlite3_prepare() function and change its third argument to -1 instead of 0. Finally install
DBD::SQLite using make and do make install to resolve the problem.
INSERT Operation
Following Perl program shows how we can create records in our COMPANY table created in above
example:
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "SQLite";
my $database = "test.db";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfullyn";
my $stmt = qq(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 ));
my $rv = $dbh->do($stmt) or die $DBI::errstr;
my $rv = $dbh->do($stmt) or die $DBI::errstr;
$stmt = qq(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Allen', 25, 'Texas', 15000.00 ));
$rv = $dbh->do($stmt) or die $DBI::errstr;
$stmt = qq(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Teddy', 23, 'Norway', 20000.00 ));
$rv = $dbh->do($stmt) or die $DBI::errstr;
$stmt = qq(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 ););
$rv = $dbh->do($stmt) or die $DBI::errstr;
print "Records created successfullyn";
$dbh->disconnect();
When above program is executed, it will create given records in COMPANY table and will display
following two line:
Opened database successfully
Records created successfully
SELECT Operation
Following Perl program shows how we can fetch and display records from our COMPANY table
created in above example:
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "SQLite";
my $database = "test.db";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfullyn";
my $stmt = qq(SELECT id, name, address, salary from COMPANY;);
my $sth = $dbh->prepare( $stmt );
my $rv = $sth->execute() or die $DBI::errstr;
if($rv < 0){
print $DBI::errstr;
}
while(my @row = $sth->fetchrow_array()) {
print "ID = ". $row[0] . "n";
print "NAME = ". $row[1] ."n";
print "ADDRESS = ". $row[2] ."n";
print "SALARY = ". $row[3] ."nn";
}
print "Operation done successfullyn";
$dbh->disconnect();
When above program is executed, it will produce following result:
Opened database successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000
Operation done successfully
UPDATE Operation
Following Perl code shows how we can use UPDATE statement to update any record and then
fetch and display updated records from our COMPANY table:
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "SQLite";
my $database = "test.db";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfullyn";
my $stmt = qq(UPDATE COMPANY set SALARY = 25000.00 where ID=1;);
my $rv = $dbh->do($stmt) or die $DBI::errstr;
if( $rv < 0 ){
print $DBI::errstr;
}else{
print "Total number of rows updated : $rvn";
}
$stmt = qq(SELECT id, name, address, salary from COMPANY;);
my $sth = $dbh->prepare( $stmt );
$rv = $sth->execute() or die $DBI::errstr;
if($rv < 0){
print $DBI::errstr;
}
while(my @row = $sth->fetchrow_array()) {
print "ID = ". $row[0] . "n";
print "NAME = ". $row[1] ."n";
print "ADDRESS = ". $row[2] ."n";
print "SALARY = ". $row[3] ."nn";
}
print "Operation done successfullyn";
$dbh->disconnect();
When above program is executed, it will produce following result:
Opened database successfully
Total number of rows updated : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000
Operation done successfully
DELETE Operation
Following Perl code shows how we can use DELETE statement to delete any record and then fetch
and display remaining records from our COMPANY table:
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "SQLite";
my $database = "test.db";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfullyn";
my $stmt = qq(DELETE from COMPANY where ID=2;);
my $rv = $dbh->do($stmt) or die $DBI::errstr;
if( $rv < 0 ){
print $DBI::errstr;
}else{
print "Total number of rows deleted : $rvn";
}
$stmt = qq(SELECT id, name, address, salary from COMPANY;);
my $sth = $dbh->prepare( $stmt );
$rv = $sth->execute() or die $DBI::errstr;
if($rv < 0){
print $DBI::errstr;
}
while(my @row = $sth->fetchrow_array()) {
print "ID = ". $row[0] . "n";
print "NAME = ". $row[1] ."n";
print "ADDRESS = ". $row[2] ."n";
print "SALARY = ". $row[3] ."nn";
}
print "Operation done successfullyn";
$dbh->disconnect();
When above program is executed, it will produce following result:
Opened database successfully
Total number of rows deleted : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000
Operation done successfully
Sqlite perl

More Related Content

PDF
PHP Data Objects
KEY
Php 101: PDO
PPTX
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
PPT
Quebec pdo
PDF
Stored Procedure
PPT
Database presentation
PDF
PHP and Mysql
PHP Data Objects
Php 101: PDO
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
Quebec pdo
Stored Procedure
Database presentation
PHP and Mysql

What's hot (20)

PPT
MYSQL - PHP Database Connectivity
PDF
Advanced Querying with CakePHP 3
PDF
CakeFest 2013 keynote
PDF
Doctrine fixtures
PDF
Dependency Injection
PDF
Neatly folding-a-tree
PPTX
Drupal7 dbtng
PDF
Lithium: The Framework for People Who Hate Frameworks
PDF
Future of HTTP in CakePHP
PDF
New in cakephp3
ODP
Database Connection With Mysql
PPT
PHP - PDO Objects
PDF
The History of PHPersistence
PDF
The Origin of Lithium
PDF
Database Design Patterns
PDF
Agile database access with CakePHP 3
PDF
The Zen of Lithium
PPTX
Mysql
PDF
Introduction to php database connectivity
PDF
Dependency Injection with PHP and PHP 5.3
MYSQL - PHP Database Connectivity
Advanced Querying with CakePHP 3
CakeFest 2013 keynote
Doctrine fixtures
Dependency Injection
Neatly folding-a-tree
Drupal7 dbtng
Lithium: The Framework for People Who Hate Frameworks
Future of HTTP in CakePHP
New in cakephp3
Database Connection With Mysql
PHP - PDO Objects
The History of PHPersistence
The Origin of Lithium
Database Design Patterns
Agile database access with CakePHP 3
The Zen of Lithium
Mysql
Introduction to php database connectivity
Dependency Injection with PHP and PHP 5.3
Ad

Similar to Sqlite perl (20)

PPT
Working with databases in Perl
ODP
ODP
Database Programming with Perl and DBIx::Class
PPT
Perl DBI Scripting with the ILS
PPT
Mysql DBI
ODP
DOCX
Miniproject on Employee Management using Perl/Database.
PPT
Php Data Objects
KEY
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
PPT
Migrating from PHP 4 to PHP 5
PPT
php databse handling
PPTX
3-Chapter-Edit.pptx debre tabour university
PDF
DBIx::Class introduction - 2010
PDF
PDO Basics - PHPMelb 2014
PDF
IR SQLite Session #1
PDF
Using Perl Stored Procedures for MariaDB
PPTX
working with PHP & DB's
PDF
BITS: Introduction to relational databases and MySQL - Schema design
Working with databases in Perl
Database Programming with Perl and DBIx::Class
Perl DBI Scripting with the ILS
Mysql DBI
Miniproject on Employee Management using Perl/Database.
Php Data Objects
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Migrating from PHP 4 to PHP 5
php databse handling
3-Chapter-Edit.pptx debre tabour university
DBIx::Class introduction - 2010
PDO Basics - PHPMelb 2014
IR SQLite Session #1
Using Perl Stored Procedures for MariaDB
working with PHP & DB's
BITS: Introduction to relational databases and MySQL - Schema design
Ad

More from Ashoka Vanjare (20)

PDF
Tika tutorial
PDF
Sqoop tutorial
PDF
Xpath tutorial
PDF
Xml tutorial
PDF
Xsd tutorial
PDF
Xslt tutorial
PDF
Xquery xpath
PDF
Postgresql tutorial
PDF
Postgresql quick guide
PDF
Perl tutorial final
PDF
PDF
Php7 tutorial
PDF
Mongodb tutorial
PDF
Maven tutorial
PDF
Mahout tutorial
PDF
Learn embedded systems tutorial
PDF
Learn data structures algorithms tutorial
PDF
Learn c standard library
PDF
Learn c programming
PDF
Json tutorial
Tika tutorial
Sqoop tutorial
Xpath tutorial
Xml tutorial
Xsd tutorial
Xslt tutorial
Xquery xpath
Postgresql tutorial
Postgresql quick guide
Perl tutorial final
Php7 tutorial
Mongodb tutorial
Maven tutorial
Mahout tutorial
Learn embedded systems tutorial
Learn data structures algorithms tutorial
Learn c standard library
Learn c programming
Json tutorial

Recently uploaded (20)

PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
PPT on Performance Review to get promotions
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPT
Project quality management in manufacturing
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
additive manufacturing of ss316l using mig welding
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
DOCX
573137875-Attendance-Management-System-original
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Internet of Things (IOT) - A guide to understanding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Model Code of Practice - Construction Work - 21102022 .pdf
PPT on Performance Review to get promotions
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Project quality management in manufacturing
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
additive manufacturing of ss316l using mig welding
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
OOP with Java - Java Introduction (Basics)
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Mechanical Engineering MATERIALS Selection
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
R24 SURVEYING LAB MANUAL for civil enggi
573137875-Attendance-Management-System-original
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Internet of Things (IOT) - A guide to understanding

Sqlite perl

  • 1. http://www.tutorialspoint.com/sqlite/sqlite_perl.htm Copyright © tutorialspoint.com SQLITE PERL TUTORIAL Installation The SQLite3 can be integrated with Perl using Perl DBI module which is a database access module for the Perl programming language. It defines a set of methods, variables and conventions that provide a standard database interface. Here are simple steps to install DBI module on your Linux/Unix machine: $ wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.625.tar.gz $ tar xvfz DBI-1.625.tar.gz $ cd DBI-1.625 $ perl Makefile.PL $ make $ make install If you need to install SQLite driver for DBI, then it can be installed as follows: $ wget http://search.cpan.org/CPAN/authors/id/M/MS/MSERGEANT/DBD-SQLite-1.11.tar.gz $ tar xvfz DBD-SQLite-1.11.tar.gz $ cd DBD-SQLite-1.11 $ perl Makefile.PL $ make $ make install DBI Interface APIs Following are important DBI routines which can sufice your requirement to work with SQLite database from your Perl program. If you are looking for a more sophisticated application then you can look into Perl DBI official documentation. S.N. API & Description 1 DBI->connect($data_source, "", "", %attr) Establishes a database connection, or session, to the requested $data_source. Returns a database handle object if the connection succeeds. Datasource has the form like : DBI:SQLite:dbname='test.db' SQLite is SQLite driver name and test.db is the name of SQLite database file. If the filename is given as ':memory:', it will create an in-memory database in RAM that lasts only for the duration of the session. If filename is actual device file name, then it attempts to open the database file by using its value. If no file by that name exists then a new database file by that name gets created. You keep second and third paramter as blank strings and last parameter is to pass various attributes as shown below in the example. 2 $dbh->do($sql) This routine prepares and executes a single SQL statement. Returns the number of rows affected or undef on error. A return value of -1 means the number of rows is not known, not applicable, or not available. Here $dbh is a handle returned by DBI->connect() call. 3 $dbh->prepare($sql)
  • 2. This routine prepares a statement for later execution by the database engine and returns a reference to a statement handle object. 4 $sth->execute() This routine performs whatever processing is necessary to execute the prepared statement. An undef is returned if an error occurs. A successful execute always returns true regardless of the number of rows affected. Here $sth is a statement handle returned by $dbh->prepare($sql) call. 5 $sth->fetchrow_array() This routine fetches the next row of data and returns it as a list containing the field values. Null fields are returned as undef values in the list. 6 $DBI::err This is equivalent to $h->err, where $h is any of the handle types like $dbh, $sth, or $drh. This returns native database engine error code from the last driver method called. 7 $DBI::errstr This is equivalent to $h->errstr, where $h is any of the handle types like $dbh, $sth, or $drh. This returns the native database engine error message from the last DBI method called. 8 $dbh->disconnect() This routine closes a database connection previously opened by a call to DBI- >connect(). Connecting To Database Following Perl code shows how to connect to an existing database. If database does not exist, then it will be created and finally a database object will be returned. #!/usr/bin/perl use DBI; use strict; my $driver = "SQLite"; my $database = "test.db"; my $dsn = "DBI:$driver:dbname=$database"; my $userid = ""; my $password = ""; my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr; print "Opened database successfullyn"; Now let's run above program to create our database test.db in the current directory. You can change your path as per your requirement. Keep above code in sqlite.pl file and execute it as shown below. If database is successfully created then it will give following message: $ chmod +x sqlite.pl $ ./sqlite.pl Open database successfully
  • 3. Create a Table Following Perl program will be used to create a table in previously created database: #!/usr/bin/perl use DBI; use strict; my $driver = "SQLite"; my $database = "test.db"; my $dsn = "DBI:$driver:dbname=$database"; my $userid = ""; my $password = ""; my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr; print "Opened database successfullyn"; my $stmt = qq(CREATE TABLE COMPANY (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL);); my $rv = $dbh->do($stmt); if($rv < 0){ print $DBI::errstr; } else { print "Table created successfullyn"; } $dbh->disconnect(); When above program is executed, it will create COMPANY table in your test.db and it will display following messages: Opened database successfully Table created successfully NOTE: in case you see following error in any of the operation: DBD::SQLite::st execute failed: not an error(21) at dbdimp.c line 398 In this case you will have open dbdimp.c file available in DBD-SQLite installation and find out sqlite3_prepare() function and change its third argument to -1 instead of 0. Finally install DBD::SQLite using make and do make install to resolve the problem. INSERT Operation Following Perl program shows how we can create records in our COMPANY table created in above example: #!/usr/bin/perl use DBI; use strict; my $driver = "SQLite"; my $database = "test.db"; my $dsn = "DBI:$driver:dbname=$database"; my $userid = ""; my $password = ""; my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr; print "Opened database successfullyn"; my $stmt = qq(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Paul', 32, 'California', 20000.00 )); my $rv = $dbh->do($stmt) or die $DBI::errstr;
  • 4. my $rv = $dbh->do($stmt) or die $DBI::errstr; $stmt = qq(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25, 'Texas', 15000.00 )); $rv = $dbh->do($stmt) or die $DBI::errstr; $stmt = qq(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )); $rv = $dbh->do($stmt) or die $DBI::errstr; $stmt = qq(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );); $rv = $dbh->do($stmt) or die $DBI::errstr; print "Records created successfullyn"; $dbh->disconnect(); When above program is executed, it will create given records in COMPANY table and will display following two line: Opened database successfully Records created successfully SELECT Operation Following Perl program shows how we can fetch and display records from our COMPANY table created in above example: #!/usr/bin/perl use DBI; use strict; my $driver = "SQLite"; my $database = "test.db"; my $dsn = "DBI:$driver:dbname=$database"; my $userid = ""; my $password = ""; my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr; print "Opened database successfullyn"; my $stmt = qq(SELECT id, name, address, salary from COMPANY;); my $sth = $dbh->prepare( $stmt ); my $rv = $sth->execute() or die $DBI::errstr; if($rv < 0){ print $DBI::errstr; } while(my @row = $sth->fetchrow_array()) { print "ID = ". $row[0] . "n"; print "NAME = ". $row[1] ."n"; print "ADDRESS = ". $row[2] ."n"; print "SALARY = ". $row[3] ."nn"; } print "Operation done successfullyn"; $dbh->disconnect(); When above program is executed, it will produce following result: Opened database successfully ID = 1 NAME = Paul ADDRESS = California SALARY = 20000 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000
  • 5. ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000 Operation done successfully UPDATE Operation Following Perl code shows how we can use UPDATE statement to update any record and then fetch and display updated records from our COMPANY table: #!/usr/bin/perl use DBI; use strict; my $driver = "SQLite"; my $database = "test.db"; my $dsn = "DBI:$driver:dbname=$database"; my $userid = ""; my $password = ""; my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr; print "Opened database successfullyn"; my $stmt = qq(UPDATE COMPANY set SALARY = 25000.00 where ID=1;); my $rv = $dbh->do($stmt) or die $DBI::errstr; if( $rv < 0 ){ print $DBI::errstr; }else{ print "Total number of rows updated : $rvn"; } $stmt = qq(SELECT id, name, address, salary from COMPANY;); my $sth = $dbh->prepare( $stmt ); $rv = $sth->execute() or die $DBI::errstr; if($rv < 0){ print $DBI::errstr; } while(my @row = $sth->fetchrow_array()) { print "ID = ". $row[0] . "n"; print "NAME = ". $row[1] ."n"; print "ADDRESS = ". $row[2] ."n"; print "SALARY = ". $row[3] ."nn"; } print "Operation done successfullyn"; $dbh->disconnect(); When above program is executed, it will produce following result: Opened database successfully Total number of rows updated : 1 ID = 1 NAME = Paul ADDRESS = California SALARY = 25000 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000 ID = 3 NAME = Teddy ADDRESS = Norway
  • 6. SALARY = 20000 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000 Operation done successfully DELETE Operation Following Perl code shows how we can use DELETE statement to delete any record and then fetch and display remaining records from our COMPANY table: #!/usr/bin/perl use DBI; use strict; my $driver = "SQLite"; my $database = "test.db"; my $dsn = "DBI:$driver:dbname=$database"; my $userid = ""; my $password = ""; my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr; print "Opened database successfullyn"; my $stmt = qq(DELETE from COMPANY where ID=2;); my $rv = $dbh->do($stmt) or die $DBI::errstr; if( $rv < 0 ){ print $DBI::errstr; }else{ print "Total number of rows deleted : $rvn"; } $stmt = qq(SELECT id, name, address, salary from COMPANY;); my $sth = $dbh->prepare( $stmt ); $rv = $sth->execute() or die $DBI::errstr; if($rv < 0){ print $DBI::errstr; } while(my @row = $sth->fetchrow_array()) { print "ID = ". $row[0] . "n"; print "NAME = ". $row[1] ."n"; print "ADDRESS = ". $row[2] ."n"; print "SALARY = ". $row[3] ."nn"; } print "Operation done successfullyn"; $dbh->disconnect(); When above program is executed, it will produce following result: Opened database successfully Total number of rows deleted : 1 ID = 1 NAME = Paul ADDRESS = California SALARY = 25000 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000 Operation done successfully