print records that match regexp
- From: bbrecht56@xxxxxxxxx
- Date: Mon, 28 Jan 2008 13:12:26 -0800 (PST)
I have a table "customer_table" with the following fields:
Id int,
firstname varchar(64),
lastname varchar(64),
emailaddress varchar(64) not null primary key
city varchar (32),
Can some one help me and show me how to print only records that
matches a given regexp using, for example if I run:
#> getRecord.pl A+
should return all record from the database if the first name, last
name, or email address starts with a capital A
OR:
#> getRecord.pl
should return all records from the table, which I have it this way and
it works just fine:
use DBI;
my $dataBaseDriver = "DBI:mysql";
my $dataBaseName = "test";
my $dataBaseUser = "user";
my $dbUserPass = "user1234";
my $tableName = "my_table";
#---------------------------------------#
# connect to the database #
#---------------------------------------#
my $dbh = DBI->connect("$dataBaseDriver:$dataBaseName",
$dataBaseUser,
$dbUserPass,
{ RaiseError => 1,
AutoCommit => 0 }) || die "ERROR-1: Couldn't
connect to $dataBaseHost: " . DBI->errstr();
print "Connection to the database successful.\n";
#---------------------------------------#
# querying the database #
#---------------------------------------#
my $sth = $dbh->prepare("SELECT * FROM $tableName");
$sth->execute();
#---------------------------------------#
# print the data #
#---------------------------------------#
while(my $ref = $sth->fetchrow_hashref()) {
print "ID: $ref->{'Id'}\n";
print "First Name: $ref->{'firstname'}\n";
print "Last Name: $ref->{'lastname'}\n";
print "Email Address: $ref->{'emailaddress'}\n";
print "City: $ref->{'city'}\n";
}
$sth->finish();
$dbh -> disconnect();
Thanks for your help
Berti
.
- Follow-Ups:
- Re: print records that match regexp
- From: Chas. Owens
- Re: print records that match regexp
- Prev by Date: Re: How to read an rfc spec
- Next by Date: Database persistnce with Perl
- Previous by thread: Want to access Jenda as a PPM repository
- Next by thread: Re: print records that match regexp
- Index(es):
Relevant Pages
|