Perl DBI 入门 -02
时间:2007-03-09 来源:darrenshen
'jerrypassword',
{
RaiseError => 1,
AutoCommit => 0
}
)
|| die "Database connection not made: $DBI::errstr";
my $sql = qq{ CREATE TABLE employees ( id INTEGER NOT NULL,
name VARCHAR(128),
title VARCHAR(128),
phone CHAR(10)
) };
$dbh->do( $sql );
$dbh->commit();
$dbh->disconnect();
进阶功能
我们已经学会了连线资料库,错误侦测,以及下达简单的 SQL 述的方法. 是该学一些更有用的程式语法的时候了.
SELECT 述
在 SQL 的述中,最常使用的指令莫过於 SELECT 述. 为了使用 SELECT,我们应该先 prepare 这一段述,然後进行 execute 动作. 在下面的程式片段中,我们都会使用 statement handle $sth 有存取 SELECT 的结果.
use strict;
use DBI;
my $dbh = DBI->connect( 'dbi:DBMaker:dbsample',
'jerry',
'jeerrypassword',
{
RaiseError => 1,
AutoCommit => 0
}
)
|| die "Database connection not made: $DBI::errstr";
my $sql = qq{ SELECT * FROM employees };
my $sth = $dbh->prepare( $sql );
$sth->execute();
$dbh->disconnect();
上述程式要求 DBMaker 资料库为查询指令先准备好执行计画,之後再执行该查询指令. 到目前为止还没有任何一笔记录传回. 稍後我们会使用 bind_columns 的技术以取得资料库输出的记录. bind_columns 分别将每个输出栏位结到一个 scalar reference. 一旦呼叫到 fetch 时,这些 scalars 就会填入这资料库传回的值.
use strict;
use DBI;
my $dbh = DBI->connect( 'dbi:DBMaker:dbsample',
'jerry',
'jerrypassword',
{
RaiseError => 1,
AutoCommit => 0
}
)
|| die "Database connection not made: $DBI::errstr";
{
RaiseError => 1,
AutoCommit => 0
}
)
|| die "Database connection not made: $DBI::errstr";
my $sql = qq{ CREATE TABLE employees ( id INTEGER NOT NULL,
name VARCHAR(128),
title VARCHAR(128),
phone CHAR(10)
) };
$dbh->do( $sql );
$dbh->commit();
$dbh->disconnect();
进阶功能
我们已经学会了连线资料库,错误侦测,以及下达简单的 SQL 述的方法. 是该学一些更有用的程式语法的时候了.
SELECT 述
在 SQL 的述中,最常使用的指令莫过於 SELECT 述. 为了使用 SELECT,我们应该先 prepare 这一段述,然後进行 execute 动作. 在下面的程式片段中,我们都会使用 statement handle $sth 有存取 SELECT 的结果.
use strict;
use DBI;
my $dbh = DBI->connect( 'dbi:DBMaker:dbsample',
'jerry',
'jeerrypassword',
{
RaiseError => 1,
AutoCommit => 0
}
)
|| die "Database connection not made: $DBI::errstr";
my $sql = qq{ SELECT * FROM employees };
my $sth = $dbh->prepare( $sql );
$sth->execute();
$dbh->disconnect();
上述程式要求 DBMaker 资料库为查询指令先准备好执行计画,之後再执行该查询指令. 到目前为止还没有任何一笔记录传回. 稍後我们会使用 bind_columns 的技术以取得资料库输出的记录. bind_columns 分别将每个输出栏位结到一个 scalar reference. 一旦呼叫到 fetch 时,这些 scalars 就会填入这资料库传回的值.
use strict;
use DBI;
my $dbh = DBI->connect( 'dbi:DBMaker:dbsample',
'jerry',
'jerrypassword',
{
RaiseError => 1,
AutoCommit => 0
}
)
|| die "Database connection not made: $DBI::errstr";
相关阅读 更多 +