EX.NO:4b
DATABASE CONNECTIVITY IN PERL
24.07.12
AIM:
To learn how to do database
connectivity in perl using simple table creation.
PROGRAM
use DBI;
use DBD::mysql;
# CONFIG VARIABLES
$platform = "mysql";
$database = "test06";
$host = "localhost";
$port = "3306";
$tablename = "writer";
$user = "2910006";
$pw = "cse006";
#DATA SOURCE NAME
$dsn =
"dbi:mysql:$database:localhost:3306";
# PERL DBI CONNECT
$connect = DBI->connect($dsn, $user,
$pw);
print "\n sucessfully
connected\n";
# PREPARE THE QUERY
$query = "SELECT * FROM Writer
ORDER BY Name";
$query_handle =
$connect->prepare($query);
# EXECUTE THE QUERY
$query_handle->execute();
# BIND TABLE COLUMNS TO VARIABLES
$query_handle->bind_columns(undef,
\$Id, \$Name);
# LOOP THROUGH RESULTS
print "Id \t Name\n " ;
while($query_handle->fetch()) {
print " $Id \t $Name \n>";
}
OUTPUT:
[root@localhost Desktop]# vi
database.pl
[root@localhost Desktop]# perl
database.pl
sucessfully connected
Id Name
> 4 bala
> 1 karthi
> 3 saravana
> 2 sasi
>[root@localhost Desktop]#
No comments:
Post a Comment