• 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);

    }
  • 相关阅读:
    vue 按钮权限
    ERP为化工企业的采购决策提供技术支持
    浅谈线性化
    三维重建_表面重建_基于符号距离场的表面重建
    vue-pdf结合alloyfinger手势缩放旋转上下翻页pdf文件
    【华为OD机试真题 python】九宫格按键输入法【2022 Q4 | 200分】
    Java项目防止SQL注入的四种方案
    疫情期间闲来无事,我自制了一个按钮展示框特效来展示我的博客
    振弦采集模块测量振弦传感器的流程步骤?
    Linux 图形栈从入门到放弃 --- Linux 图形相关概念简介
  • 原文地址:https://blog.csdn.net/vempire/article/details/128077016