GeekInterview.com
Series: Subject: Topic:
Question: 60 of 65

How to Connect with SqlServer from perl and how to display database table info?

Asked by: Interview Candidate | Asked on: Mar 9th, 2006
Showing Answers 1 - 3 of 3 Answers
siva rajesh

Answered On : Mar 17th, 2006

Dear folks, there is a module in perl named DBI - Database independent interface which will be used to connect to any database by using same code. Along with DBI we should use database specific module here it is SQL server. for MSaccess it is DBD::ODBC, for MySQL it is DBD::mysql driver, for integrating oracle with perl use DBD::oracle driver is used. IIy for SQL server there are avilabale many custom defined ppm( perl package manager) like Win32::ODBC, mssql::oleDB etc.so, together with DBI, mssql::oleDB we can access SQL server database from perl. the commands to access database is same for any database. for furhter help needed, u can mail me-Rajesh

Yes  1 User has rated as useful.
  
Login to rate this answer.
madhu

Answered On : Nov 17th, 2006

hi,

In perl we have DBI module to connect to any database.

step1: we need to intialise the DBI module

use DBI;

step2 : connect to database using DBI module which reutrns database handle

$dh = DBI->connect("dbi:mysql:database=DBname","username","password");

step3 : to rertive data from database use select query

$sth = $dh->prepare("select name,symbol from table");

$sth->execute();

while(@row = $sth->fetchrow_array()){

print "name =$row[0],symbol = $row[1];

}

$dh->disconnect();

Yes  1 User has rated as useful.
  
Login to rate this answer.
Rob

Answered On : Apr 9th, 2007

The other trick is to use http://www.freetds.org/ for your sybase driver (M$SQL is just an old version of sybase that has been heavily modified).  Then standard DBI calls work.  Here is a program to login to an SQL server and print success if login is ok:

use DBI;
use DBD::FreeTDS;
$opt_u = "USERNAME";
$opt_p = "PASSWORD";
$opt_s = "SERVERNAME:1433";

$dbh = DBI->connect("dbi:FreeTDS:server=$opt_s", $opt_u, $opt_p);
$error = $DBI::errstr;

if (defined($dbh)) {
   # SUCCESS
   print "Login goodn";
} else {
   # ERROR
   print "$errorn";
}

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.