mysql结果上的perl数组搜索

mysql结果上的perl数组搜索,mysql,arrays,linux,perl,Mysql,Arrays,Linux,Perl,我有一个包含手机号码的数据库。如何编写将所有数字放入数组的perl脚本&检查该数组中是否存在新的数字 创建表格: CREATE TABLE consumeruser ( ConsumerId int(10) NOT NULL AUTO_INCREMENT, ConsumerName varchar(45) DEFAULT NULL, ConsumerMobNo varchar(10) DEFAULT NULL, PRIMARY KEY (ConsumerId) ) ENG

我有一个包含手机号码的数据库。如何编写将所有数字放入数组的perl脚本&检查该数组中是否存在新的数字

创建表格:

CREATE TABLE consumeruser (
  ConsumerId    int(10) NOT NULL AUTO_INCREMENT,
  ConsumerName  varchar(45) DEFAULT NULL,
  ConsumerMobNo varchar(10) DEFAULT NULL,
  PRIMARY KEY (ConsumerId)
) ENGINE=InnoDB AUTO_INCREMENT=4494 DEFAULT CHARSET=latin1
#!/usr/bin/perl -w
use strict;
use warnings qw(all);

use DBI;
use Getopt::Long;
use Pod::Usage;
use Text::CSV_XS;

my $username = 'root';         # set your MySQL username
my $password = 'xxxx';         # set your MySQL password
my $database = 'app';          # set your MySQL database name
my $server   = 'localhost';    # set your server hostname (probably localhost)

my $dbh = DBI->connect( "DBI:mysql:$database;host=$server", $username, $password )
    || die "Could not connect to database: $DBI::errstr";

my $CustomerMobileNumber = 9999999;
my @MobileNumbers;
my $mobileNumberQuery = "select ConsumerMobNo from consumeruser";
my $sth               = $dbh->prepare($mobileNumberQuery);
$sth->execute();
while ( my @row = $sth->fetchrow_array() ) {
    push @MobileNumbers, $row;

    if (/test for is present/) {
        #9999999 found in array;
    } else {
        #9999999 not found in array;
    }
}
脚本:

CREATE TABLE consumeruser (
  ConsumerId    int(10) NOT NULL AUTO_INCREMENT,
  ConsumerName  varchar(45) DEFAULT NULL,
  ConsumerMobNo varchar(10) DEFAULT NULL,
  PRIMARY KEY (ConsumerId)
) ENGINE=InnoDB AUTO_INCREMENT=4494 DEFAULT CHARSET=latin1
#!/usr/bin/perl -w
use strict;
use warnings qw(all);

use DBI;
use Getopt::Long;
use Pod::Usage;
use Text::CSV_XS;

my $username = 'root';         # set your MySQL username
my $password = 'xxxx';         # set your MySQL password
my $database = 'app';          # set your MySQL database name
my $server   = 'localhost';    # set your server hostname (probably localhost)

my $dbh = DBI->connect( "DBI:mysql:$database;host=$server", $username, $password )
    || die "Could not connect to database: $DBI::errstr";

my $CustomerMobileNumber = 9999999;
my @MobileNumbers;
my $mobileNumberQuery = "select ConsumerMobNo from consumeruser";
my $sth               = $dbh->prepare($mobileNumberQuery);
$sth->execute();
while ( my @row = $sth->fetchrow_array() ) {
    push @MobileNumbers, $row;

    if (/test for is present/) {
        #9999999 found in array;
    } else {
        #9999999 not found in array;
    }
}

您可以要求数据库搜索该号码:

my $mobileNumberQuery = "SELECT 1 FROM consumeruser WHERE ConsumerMobNo = ?";
my $sth = $dbh->prepare($mobileNumberQuery);
$sth->execute(9999999);
if ($sth->fetchrow_array) {
    print "Found.\n"
} else {
    print "Not found.\n";
}

使用SELECT查询查看数据库中是否存在该号码,而不是将所有号码从数据库中取出并放入一个数组。实际上,我有一个CSV文件,其中包含移动号码,我想通过数据库检查移动号码,因此我想避免对每个移动号码进行SELECT查询