用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - perl代码库

perl 使用DBI和DBD执行一个SQL命令

2012-10-14 作者: 神马举报

[perl]代码库

#-----------------------------
use DBI;


$dbh = DBI->connect ( 'DBI:driver:database', 'username', 'auth',

                      { RaiseError => 1, AutoCommit => 1} );

$dbh->do ( $sql );

$sth = $dbh->prepare ( $sql );

$sth->execute();

while ( @row = $sth->fetchrow_array )
{

# ...

}

$sth->finish();

$dbh->disconnect();
#-----------------------------
#disconnect(DBI::db=HASH(0x9df84)) invalidates 1 active cursor(s)
#    at -e line 1.
#-----------------------------
# download the following standalone program
#!/usr/bin/perl -w
# dbusers - manage MySQL user table
use DBI;
use User::pwent;

$dbh = DBI->connect ( 'DBI:mysql:dbname:mysqlserver.domain.com:3306',
                      'user', 'password',
                      { RaiseError => 1 } )
       or die "connecting : $DBI::errstr\n";

$dbh->do ( "CREATE TABLE users (uid INT, login CHAR(8))" );

$sql_fmt = "INSERT INTO users VALUES( %d, %s )";
while ( $user = getpwent )
{
	$sql = sprintf ( $sql_fmt, $user->uid, $dbh->quote ( $user->name ) );
	$dbh->do ( $sql );
}

$sth = $dbh->prepare ( "SELECT * FROM users WHERE uid < 50" );
$sth->execute;

while ( ( @row ) = $sth->fetchrow )
{
	print join ( ", ", map {defined $_ ? $_ : "(null)"} @row ), "\n";
}
$sth->finish;

$dbh->do ( "DROP TABLE users" );

$dbh->disconnect;

#-----------------------------


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...