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

Showing Answers 1 - 3 of 3 Answers

siva rajesh

  • 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

madhu

  • 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();

Rob

  • 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";
}

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions