Perl DBI MySQL数据库访问示例_MySQL
#! /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 );
登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
1 个月前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保护它?
1 个月前
By DDD
R.E.P.O.最佳图形设置
2 周前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
1 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文讨论了使用MySQL的Alter Table语句修改表,包括添加/删除列,重命名表/列以及更改列数据类型。

文章讨论了为MySQL配置SSL/TLS加密,包括证书生成和验证。主要问题是使用自签名证书的安全含义。[角色计数:159]

文章讨论了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比较了它们对初学者和高级用户的功能和适合性。[159个字符]

本文讨论了使用Drop Table语句在MySQL中放下表,并强调了预防措施和风险。它强调,没有备份,该动作是不可逆转的,详细介绍了恢复方法和潜在的生产环境危害。

本文讨论了在PostgreSQL,MySQL和MongoDB等各个数据库中的JSON列上创建索引,以增强查询性能。它解释了索引特定的JSON路径的语法和好处,并列出了支持的数据库系统。

文章讨论了使用准备好的语句,输入验证和强密码策略确保针对SQL注入和蛮力攻击的MySQL。(159个字符)
