Perl DBI MySQL数据库访问示例_MySQL

php中文网
发布: 2016-06-01 13:10:09
原创
1840人浏览过

#! /usr/bin/perl -wuse strict;use DBI;# 创建mysql连接sub create_conn    # returns connection handle{ my $dsn = "DBI:mysql:test:127.0.0.1:3306"; my $user = "test"; my $pass = "password"; my $dbh = DBI->connect($dsn, $user, $pass, {RaiseError=>0, PrintError => 1})  or die "Could not connect to mysql server: $DBI::err($DBI::errstr)/n";}# 使用fetchrow_array获取记录并打印sub fetch_and_print_results  # params: stmt handle{ my $sth = shift(@_); while (my @row = $sth->fetchrow_array()) {  print join("/t", @row), "/n"; }}# 使用fetchrow_arrayref获取记录引用并打印sub fetch_and_print_results2  # params: stmt handle{ my $sth = shift(@_); while (my $rowref = $sth->fetchrow_arrayref()) {  my $delim = "";  for( my $i = 0; $i < @{$rowref}; ++$i)  {   #$rowref->{$i} = " " if !defined ($rowref->{$i}); # NULL to space   print $delim . @{$rowref}[$i];   $delim = ',';  }  print "/n"; }}# 使用fetchrow_hashref获取记录hash表引用并打印sub fetch_and_print_results3  # params: stmt handle{ my $sth = shift(@_); my $labels = $sth->{NAME}; my $cols = $sth->{NUM_OF_FIELDS};  print ">>>> field count $cols/n"; while (my $rowref = $sth->fetchrow_hashref() ) {  my $delim = "";  for( my $i = 0; $i < $cols; ++$i)  {   print $delim . $labels->[$i]. ' = '.%{$rowref}->{$labels->[$i]};   $delim = ',';  }  print "/n"; }}# 删除表中所有记录sub test_clear_table{ my $dbh = create_conn; my $rows = $dbh->do(qq/delete from member/);  # 直接执行删除语句,并返回删除记录数 print ">>>> total $rows records deleted/n"; $dbh->disconnect;}# 数据插入与查询sub test_insert_and_select{ my $dbh = create_conn();  # 执行数据插入语句 print '>>>>>>>> test_insert_and_select'."/n"; my $sql_insert = "insert into member (username, password) values ('julia', 'roberts')"; my $rows = $dbh->do($sql_insert); print "$rows row(s) inserted/n";  # 执行查询语句 print ">>>> fetch result 1/n"; my $sth = $dbh->prepare("select * from member"); $sth->execute; fetch_and_print_results $sth; $sth->finish;  print ">>>> fetch result 2/n"; $sth = $dbh->prepare("select username, password from member"); $sth->execute; fetch_and_print_results2 $sth; $sth->finish;  # 执行带参数的查询语句 print ">>>> fetch result 3/n"; $sth = $dbh->prepare("select username, password from member where username = ?"); $sth->execute('julia'); fetch_and_print_results3 $sth; $sth->finish;  $dbh->disconnect;}# 参数化insert语句执行sub test_param_insert{ my $dbh = create_conn(); my $sth = $dbh->prepare(qq{insert into member (username, password) values (?, ?)}); my $rows = $sth->execute('maria', 'louise'); print "$rows".' inserted: maria louise'; $sth->finish;  # undef处表示未设置的查询选项参数,不能省略 $dbh->do(qq/insert into member (username, password) values (?, ?)/, undef,          'george', 'cardon');  $dbh->disconnect;}# 绑定select输出sub test_select_out_param_bind { print ">>>>>>> test_select_param_bind/n"; my ($user, $pass); my $dbh = create_conn; my $sth = $dbh->prepare(qq{select username, password from member}); $sth->execute(); $sth->bind_col(1, /$user); $sth->bind_col(2, /$pass); print(">>1 == $user, $pass/n") while $sth->fetch();  $sth->finish();  $sth = $dbh->prepare(qq{select username, password from member}); $sth->execute(); $sth->bind_columns(/$user, /$pass); print(">>2 == $user, $pass/n") while $sth->fetch();  $sth->finish();  $dbh->disconnect;}# 事务调用sub test_transaction{ print ">>>>>>> test_transaction/n"; my $dbh = create_conn; $dbh->{AutoCommit} = 0; my $sth = $dbh->prepare(qq{insert into member (username, password) values (?,?)}); $sth->execute('tom', 'jerry'); $dbh->commit;  $sth->execute('tom', 'tom'); $dbh->rollback; $sth->finish;  $dbh->disconnect;}sub main { test_clear_table; test_param_insert; test_insert_and_select; test_transaction; test_select_out_param_bind;}exit( main );
登录后复制

Python操作Mysql实例代码教程
Python操作Mysql实例代码教程

本文介绍了Python操作MYSQL、执行SQL语句、获取结果集、遍历结果集、取得某个字段、获取表字段名、将图片插入数据库、执行事务等各种代码实例和详细介绍,代码居多,是一桌丰盛唯美的代码大餐。如果想查看在线版请访问:https://www.jb51.net/article/34102.htm

Python操作Mysql实例代码教程 0
查看详情 Python操作Mysql实例代码教程
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号