• Perl连接mysql数据库


    #!/usr/bin/perl

    # Use mandatory external modules
    use strict;
    use warnings;

    use DBI;

    my $host     = '192.168.90.X';
    my $user     = 'test';
    my $pass     = 'test';
    my $port     = '3306';
    my $database = 'extmail';
    my $table    = 'alias';


    my $filename = '/usr/local/src/user.txt';
    open FH, "< $filename" or die "can't open $filename  ..... ($!)";




    # connect to servers (this)
    our $this_dbh = &MysqlConnect( $host, $port, $user, $pass );
    print  "ERROR: Can't connect to MySQL (host = $host:$port, user = $user)!"
      if ( !$this_dbh );


    # insert user data
    foreach () {
            chomp $_;
            my $mailbox = "$_\@cisco.cn";
            my $alias_mailbox = "$_\@sales\.cisco.cn";
            print "$mailbox\n";
            print "$alias_mailbox\n";

        my $sql = "INSERT INTO $database\.$table (address, goto) VALUES ('$mailbox', '$alias_mailbox')";
        print "$sql\n";

        my $quota_status = &MysqlChange( $this_dbh, $sql );

    }



    # Get user quota
    my $sql = "select * from $database\.$table";
    print "$sql\n";

    my $user_list = &MysqlQuery( $this_dbh, $sql );
    print "$user_list\n";






    #-----------------------------------------------------------------
    sub MysqlConnect(

    " role="presentation">
    ) {
        my ( $host, $port, $user, $pass ) = @_;

        my $dsn = "DBI:mysql:host=$host;port=$port";
        return DBI->connect( $dsn, $user, $pass, { PrintError => 0 } );
    }


    #-----------------------------------------------------------------
    sub MysqlQuery(
    ) {      my ( $dbh, $query ) = @_;       my $sth = $dbh->prepare($query);      my $res = $sth->execute;      return undef unless ($res);       while ( my @row = $sth->fetchrow_array() ) {          print("@row \n");      }       $sth->finish;   }   #-----------------------------------------------------------------  sub MysqlChange(" role="presentation">) {      my ( $dbh, $query ) = @_;       my $sth = $dbh->prepare($query);      my $res = $sth->execute;      return undef unless ($res);       while ( my @row = $sth->fetchrow_array() ) {          print("@row \n");      }       $sth->finish;   }   #-----------------------------------------------------------------  sub MysqlChange(
    ) {
        my ( $dbh, $change ) = @_;
        my $sth = $dbh->do($change);

    }
  • 相关阅读:
    Spring-boot-starter-actuator的可视化spring-boot-admin
    C语言基本语法入门练习题
    LeetCode算法二叉树—144. 二叉树的前序遍历
    基于JavaWeb的物流管理系统网站设计
    微信反复读取你的相册!官方回应:为便于聊天时快速发图,最新版本将取消...
    CPU设计——Triumphcore——V2版本
    盘点近年来面试常见的spring面试真题
    vue从安装到熟练 2022流畅无痛版(第一季:入门篇)
    tail -f 、tail -F、tailf的区别
    【开题报告】基于SpringBoot的中小企业设备管理系统的设计与实现
  • 原文地址:https://blog.csdn.net/vempire/article/details/128077016