加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl + mysql

发布时间:2020-12-15 23:59:22 所属栏目:大数据 来源:网络整理
导读:strawberry #!/usr/bin/perluse strict;use DBI;# 连接到数据库,创建数据库my $dbh = DBI-connect("DBI:mysql:test:localhost","root","test",{ RaiseError = 1,AutoCommit = 0 }); $dbh-do("CREATE SCHEMA IF NOT EXISTS `gspider` DEFAULT CHARACTER SET

strawberry

#!/usr/bin/perl
use strict;
use DBI;

# 连接到数据库,创建数据库
my  $dbh = DBI->connect("DBI:mysql:test:localhost","root","test",{ RaiseError => 1,AutoCommit => 0 });
    $dbh->do("CREATE SCHEMA IF NOT EXISTS `gspider` DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci");
    $dbh->commit();
    $dbh->disconnect();
# 连接到新的数据库
my  $dbh = DBI->connect("DBI:mysql:gspider:localhost",AutoCommit => 0 });
    $dbh->do("SET character_set_connection = 'gbk' ");
    $dbh->do("SET character_set_client = 'gbk' ");
    $dbh->do("SET character_set_results = 'gbk' ");
    
# 创建表
$dbh->do("CREATE TABLE IF NOT EXISTS t (id INT PRIMARY KEY,name VARCHAR(16))");                
# 插入数据
$dbh->do("insert into t values(1,'我来了')");
$dbh->do("insert into t values(2,'good')");
$dbh->do("insert into t values(3,'g很好')");

#查询数据
my $sql = "SELECT * FROM t";
my $dbconn = $dbh->prepare($sql);
$dbconn->execute();

my $table = $dbconn->fetchall_arrayref;
my($i,$j);
for $i ( 0 .. $#{$table} ) {
    for $j ( 0 .. $#{$table->[$i]} ) {
        print "$table->[$i][$j]t";
    }
    print "n";
}

$dbh->commit();
$dbh->disconnect();
<>;


效果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读