Perl – DBI(数据库独立)模块 |套装 – 2
Perl 允许在 Perl 脚本的帮助下处理数据库。这些脚本在称为 DBI(数据库独立接口)模块的模块的帮助下运行。 DBI 模块提供了一个 API 来与 MySQL、Oracle 等许多数据库进行交互。该模块提供了一组变量和方法,这些变量和方法提供了与数据库接口的交互,并且不需要访问原始数据库。
创建表
要在 Perl 中创建表,首先我们必须在 SQL 中创建一个数据库。为此,我们将在 SQL 提示符下执行以下语句:
CREATE DATABASE EMP_DB;
现在,将创建一个名为EMP_DB的数据库。我们将使用该数据库将员工记录存储在我们的数据库中。
现在,我们将使用以下命令连接到该数据库 -
my $dbh = DBI->connect("dbi:$driver:$dsn", $username, $passwd, { AutoCommit => 1 })
现在,我们将建立与数据库的连接,我们将使用以下语句创建一个表 EMP_DEATAILS 来保存员工详细信息的记录。
my $sth = $dbh->prepare(“CREATE TABLE EMP_DETAILS ( ID INT PRIMARY KEY, EMP_NAME VARCHAR(50), AGE INT, SALARY INT)”);
$sth->execute();
完整程序供参考如下:
#!/usr/bin/perl -w
use strict;
use DBI;
my $driver = "mysql";
my $dsn = "database=EMP_DB";
my $username = "root";
my $passwd = "";
my $dbh = DBI->connect("dbi:$driver:$dsn",
$username, $passwd,
{ AutoCommit => 1 }) or
die "Failed to connect to database : $DBI::errstr";
my $sth = $dbh->prepare("CREATE TABLE EMP_DETAILS (
ID INT PRIMARY KEY,
EMP_NAME VARCHAR(50),
AGE INT, SALARY INT)");
$sth->execute();
输出:
插入数据库
现在我们已经在 EMP_DB 数据库中创建了 EMP_DETAILS 表,让我们使用以下语句将一些值插入到表中:
#$dbh->do("INSERT INTO EMP_DETAILS (ID, EMP_NAME, AGE, SALARY)
VALUES (71, 'CHINMAY', 23, 10000)")
or die "Failed to insert : $DBI::errstr";
#$dbh->do("INSERT INTO EMP_DETAILS (ID, EMP_NAME, AGE, SALARY)
VALUES (73, 'SANSKRUTI', 23, 15000)")
or die "Failed to insert : $DBI::errstr";
$dbh->do("INSERT INTO EMP_DETAILS (ID, EMP_NAME, AGE, SALARY)
VALUES (150, 'DEREK', 37, 20000)")
or die "Failed to insert : $DBI::errstr";
$dbh->do("INSERT INTO EMP_DETAILS (ID, EMP_NAME, AGE, SALARY)
VALUES (200, 'ROSS', 30, 24250)")
or die "Failed to insert : $DBI::errstr";
输出:
插入绑定值
考虑下面给出的程序。在程序中,我们在某些地方使用了问号(?)。这些问号称为占位符。通过使用占位符,数据库驱动程序可以在将查询执行到 SQL 提示符之前对其进行预处理。这个过程称为数据绑定。绑定有助于我们提高代码安全性,这有助于防止 SQL 注入攻击。
#!/usr/bin/perl -w
use strict;
use DBI;
my $driver = "mysql";
my $dsn = "database=EMP_DB";
my $username = "root";
my $passwd = "";
my $dbh = DBI->connect("dbi:$driver:$dsn",
$username, $passwd,
{ AutoCommit => 1 }) or
die "Failed to connect to database : $DBI::errstr";
# Inserting Bind Values
print "Enter Employee ID - ";
my $id = <>;
chomp($id);
print "Enter Employee Name - ";
my $name = <>;
chomp($name);
print "Enter Employee Age - ";
my $age = <>;
chomp($age);
print "Enter Employee Salary - ";
my $salary = <>;
chomp($salary);
my $sth = $dbh->prepare("INSERT INTO EMP_DETAILS (
ID, EMP_NAME, AGE, SALARY)
VALUES (?, ?, ?, ?)");
$sth->execute($id, $name, $age, $salary) or
die "Failed to insert : $DBI::errstr";
输出:
读取数据库值
我们的数据库现在填充了一些值。现在我们需要将它们读回给用户。为此,我们将使用 while 循环将数据读回给用户。
#!/usr/bin/perl -w
use strict;
use DBI;
my $driver = "mysql";
my $dsn = "database=EMP_DB";
my $username = "root";
my $passwd = "";
my $dbh = DBI->connect("dbi:$driver:$dsn",
$username, $passwd,
{ AutoCommit => 1 }) or
die "Failed to connect to database : $DBI::errstr";
my $sth = $dbh->prepare("SELECT EMP_NAME, ID FROM EMP_DETAILS WHERE ID<100");
$sth->execute() or die "Failed to select rows : $DBI::errstr";
my $rows = $sth->rows;
print "Number of rows updated : $rows \n";
while (my @emp_data = $sth->fetchrow_array())
{
my ($name, $id ) = @emp_data;
print "Employee Name = $name, Employee ID = $id \n";
}
$sth->finish();
输出:
更新数据库值
要更新表的任何值,我们可以通过以下方式使用 SQL UPDATE 命令:
#!/usr/bin/perl -w
use strict;
use DBI;
my $driver = "mysql";
my $dsn = "database=EMP_DB";
my $username = "root";
my $passwd = "";
my $dbh = DBI->connect("dbi:$driver:$dsn",
$username, $passwd,
{ AutoCommit => 1 }) or
die "Failed to connect to database : $DBI::errstr";
$dbh->do("UPDATE EMP_DETAILS SET ID=250 WHERE ID=150")
or die "Failed to update : $DBI::errstr";
输出:
原表:
数据更新后的表格:
删除数据库值
要从表中删除特定行,我们可以通过以下方式使用 SQL DELETE 语句:
#!/usr/bin/perl -w
use strict;
use DBI;
my $driver = "mysql";
my $dsn = "database=EMP_DB";
my $username = "root";
my $passwd = "";
my $dbh = DBI->connect("dbi:$driver:$dsn",
$username, $passwd, { AutoCommit => 1 }) or
die "Failed to connect to database : $DBI::errstr";
$dbh->do("DELETE FROM EMP_DETAILS WHERE ID=250") or
die "Failed to delete : $DBI::errstr";
$dbh->do("DELETE FROM EMP_DETAILS WHERE EMP_NAME='ROSS'") or
die "Failed to delete : $DBI::errstr";
输出: