1. Incerting a row
#!C:/Perl/bin/perl
use DBI;
use strict;
my $driver = "mysql";
my $database = "academia";
my $dsn = "DBI:$driver:database=$database";
my $userid = "root";
my $password = "The_password";
my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;
my $sth = $dbh->prepare("INSERT INTO physicists
VALUES (112, 'Michael', 'Faraday', '1791', '1867', 'Elecromagetism')");
$sth->execute() or die $DBI::errstr;
2. Displaying a table
my $sth = $dbh->prepare("SELECT * from physicists");
$sth->execute() or die $DBI::errstr;
my $nb_rows = $sth->rows;
print "Number of rows found : $nb_rows";
print "\n";
while (my @row = $sth->fetchrow_array()) {
my ($phys_id,$first_name,$last_name,$birth_date,$death_date,$discipline) = @row;
print " Id = $phys_id. First name = $first_name. Last name = $last_name.
Birth day = $birth_date. Death date = $death_date. Discipline = $discipline.\n";
}
$sth->finish();
3. Related output:
C:\www\PERL>perl database_3.pl
Number of rows found : 9
Id = 101. First name = Joseph. Last name = Thomson.
Birth day = 1856. Death date = 1940. Discipline = atomic.
Id = 102. First name = Ernest. Last name = Rutherford.
Birth day = 1871. Death date = 1937. Discipline = nuclear.
Id = 103. First name = Max. Last name = Planck.
Birth day = 1858. Death date = 1947. Discipline = radiation.
Id = 105. First name = Niels. Last name = Bohr.
Birth day = 1885. Death date = 1962. Discipline = atomic.
Id = 106. First name = Louis. Last name = DeBroglie.
Birth day = 1892. Death date = 1987. Discipline = wave_mechanics.
Id = 107. First name = Erwin. Last name = Schrodinger.
Birth day = 1887. Death date = 1961. Discipline = quantum_mechanics.
Id = 108. First name = Maurice. Last name = Dirac.
Birth day = 1902. Death date = 1984. Discipline = atomic.
Id = 109. First name = Ludwig. Last name = Boltzmann.
Birth day = 1844. Death date = 1906. Discipline = stat_physics.
Id = 112. First name = Michael. Last name = Faraday.
Birth day = 1791. Death date = 1867. Discipline = Elecromagetism.
C:\www\PERL>
|