CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Nt and DBI

 



pu_nk
journeyman

Feb 26, 2001, 9:40 PM

Post #1 of 2 (139 views)
Nt and DBI Can't Post

Hello,
this may be a stupid question but.......anyway
Does DBI.pm work off an NT server ?
I am working with a guy on writing a program and we want it to run off an NT server but we are having problems...

Any suggestions.. if DBI does not work on NT then what can we use to connect to a MySQL database?

Thanks in advance!
Drew



Jean
User


Feb 26, 2001, 10:24 PM

Post #2 of 2 (135 views)
Re: Nt and DBI [In reply to] Can't Post

DBI does work on my NT Workstation. It shoud work on the Server as well.
I would suggest creating a sample prog just to see that DBI works.
BTW, as on Unix, DBI requires DBD for the preferred database.

Following is the working example of using DBI with Oracle that works on NT:


Code
use DBI; 
use strict;

# Update the following 2 vars according to your needs ==>
my $ConnectStr = "sid;user;pass"; # DSN
my $SqlStmt = "SELECT column FROM table"; # SQL Query
my $dbh; # Database connection handle

############################################################################
sub DBConnect($$) # Params: 1 - DB Handle (Reference), 2 - ConnectStr
############################################################################
{
my $dbh = shift(@_);
my $str = shift(@_);
my ($db_name, $db_user, $db_pwd) = split (/;/, $str);
my %attr;

# Do not show DBI error message on the screen.
$attr{'PrintError'} = 0;
# Do not terminate program automatically (aloow use of die, warn etc).
$attr{'RaiseError'} = 0;

# Connect to the database
$$dbh = DBI->connect('dbi:Oracle:', "$db_user\@$db_name", $db_pwd, \%attr) || die "Error: Unable to connect to the user $db_user on $db_name ($DBI::errstr)\n";
}

############################################################################
sub DBExecSql($$) # Params: 1 - DB Handle (Reference), 2 - SQL Statement
############################################################################
{
my $dbh = shift(@_);
my $SqlStmt = shift(@_);
my $sth;
my @row;

$sth = $$dbh->prepare($SqlStmt) || die "Error preparing statement ($DBI::errstr).\n";
$sth->execute() || die "Error: Unable to execute statement ($DBI::errstr).\n";
# In case query returns something...
while ( @row = $sth->fetchrow_array ) {
print "@row \n";
}
}

############################################################################
sub DBDisconnect($) # Params: 1 - DB Handle (Reference)
############################################################################
{
my $dbh = shift(@_);
$$dbh->disconnect || die "Error: Disconnecting from the database failed ($DBI::errstr).\n";
}
############################################################################
# Main()
############################################################################

DBConnect (\$dbh, $ConnectStr);
DBExecSql (\$dbh, $SqlStmt);
DBDisconnect (\$dbh);

print "Done. \n";
eval `pause`;

# J.S.


Sorry for the length...
P.S. Japhy, thanks for the <pre> tip ;-)

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives