I wrote a program using dbi to keep track of all my buddies, contacts,
phone numbers , etc from different applications.
(I want to have one central source for all of the contact info that I
have for people).
I was able to parse the various data files without a problem. I then
added code to try and update an MS Access database. Before I add a new
group into the database,
I went to check to see if it is there. So I do a select, check to see if
sth->rows is 0, 1, or more. If it is 0, I insert, if it is 1, I skip
this group, and if it is more I report the error and die. So when I try
to run my program, it always die-ed. I couldn't understand why (on an
empty db), so I added a line to print out the sth->rows.
Instead of getting an integer I am getting
"DBI::st=HASH(0x1b177f8)->rows".
I am trying to figure out if there is a problem with my code or
installation of perl. I re-installed perl and dbi, and still get the
same error. Yet no one I showed my code to could figure out anything
wrong.
I have Windows XP, I am using perl 5.6.1 from ActiveState, and am trying
to connect to a MS Access database.
I am pasting code below that is my db code stripped out from my parser.
Anyone have any ideas?
Thanks,
Josh Caesar
[EMAIL PROTECTED]
------------------------------------------------------------------------
-----------------------
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
my $dbh; # database handle
my $DSN = 'dbi:ODBC:contacts';
my $username = "";
my $password = "";
my $sth; # statement handle
my $sql_check; # sql statement
# automatic error checking
my %db_attr = ( PrintError => 1, RaiseError => 1 );
# connect to the db
$dbh = DBI->connect( $DSN, $username, $password, \%db_attr )
or die "Can't connect to database: $DBI::errstr\n";
#check to see if this group exists
$sql_check = "SELECT id FROM aim_group_type WHERE name = 'Family';";
$sth = $dbh->prepare($sql_check);
unless($sth->execute)
{ $sth->finish(); $dbh->disconnect; die "SQL select see if the group
exists failed. $DBI::errstr\n$!"; }
print "DEBUG sth rows | $sth->rows |\n"; #how many rows were returned?
if ($sth->rows == 0) {
print "Got zero rows, it isn't in the db\n";
$sth->finish();
}
elsif($sth->rows == 1) {
# this group already exists
print "Got one row, it is in the db\n";
$sth->finish();
}
else {
#die too MANY rows
#always gets here and dies
$sth->finish();
$dbh->disconnect;
die "ERROR More than one group (key value) was returned, this
shouldn't be\n";
$sth->finish();
}
$dbh->disconnect;
exit;