DataBase Connections:
Code
[root@localhost student]# cat
perlupdatesasi.pl
use DBI;
use DBD::mysql;
# CONFIG VARIABLES
$platform = "mysql";
$database = "sasidb";
$host = "localhost";
$port = "3306";
$tablename = "Writers";
$user = "sasi";
$pw = "cse071";
#DATA SOURCE NAME
$dsn =
"dbi:mysql:$database:localhost:3306";
# PERL DBI CONNECT
$connect = DBI->connect($dsn, $user,
$pw);
print "\n sucessfully
connected\n";
$name=<>;
$id=<>;
$query = "UPDATE Writers SET
name='$name' WHERE id='$id'";
# PREPARE THE QUERY
$query1="select * from Writers
ORDER by Id";
$query_handle =
$connect->prepare($query);
$query_handle1 =
$connect->prepare($query1);
# EXECUTE THE QUERY
$query_handle->execute();
$query_handle1->execute();
# BIND TABLE COLUMNS TO VARIABLES
$query_handle1->bind_columns(undef,
\$Id, \$Name);
# LOOP THROUGH RESULTS
print "ID \t NAME\n " ;
while($query_handle1->fetch())
{
print " $Id \t $Name \n>";
}
[root@localhost student]# cat
perlsasidelete.pl
use DBI;
use DBD::mysql;
# CONFIG VARIABLES
$platform = "mysql";
$database = "sasidb";
$host = "localhost";
$port = "3306";
$tablename = "Writers";
$user = "sasi";
$pw = "cse071";
#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 = "DELETE from Writers
where id=5";
$query1="select * from Writers
ORDER by Id";
$query_handle =
$connect->prepare($query);
$query_handle1 =
$connect->prepare($query1);
# EXECUTE THE QUERY
$query_handle->execute();
$query_handle1->execute();
# BIND TABLE COLUMNS TO VARIABLES
$query_handle1->bind_columns(undef,
\$Id, \$Name);
# LOOP THROUGH RESULTS
print "ID \t NAME\n " ;
while($query_handle1->fetch())
{
print " $Id \t $Name \n>";
}
Output
[root@localhost student]# perl
perldb.pl
sucessfully connected
ID NAME
4 sasicse
> 5 Truman Capote
>[root@localhost student]# vi
perldeletesasi.pl
[root@localhost student]# perl
perldeletesasi.pl
sucessfully connected
ID NAME
4 sasicse
>[root@localhost student]# perl
perldb.pl
sucessfully connected
ID NAME
4 sasicse
>[root@localhost student]#
[root@localhost student]# perl
perlupdatesasi.pl
sucessfully connected
sasi
4
ID NAME
4 sasi
>[root@localhost student]#
No comments:
Post a Comment