Upload
mahinth-christensen
View
2.231
Download
0
Embed Size (px)
DESCRIPTION
DBI MySql Apache PERL
Citation preview
Saturday, April 8, 2023 Dept Of ISE, PESIT 1
MySQL
Mysql
-An open source, fast, easy to use.
-ANSI std.
-can easily be integrated into perl programs by using perl DBI-Data Base Interface module.
DBI is an API that allows perl to connect to & query number of SQL databases.
Ex. MySQL, mSQL,PostgreSQL,Oracle, Sybase, & Informix.
Saturday, April 8, 2023 Dept Of ISE, PESIT 2
MySQL
Make a connection to MySQL server as the root MySQL user.
$ mysql –u root
* MySQL root is used to administer to
MySQL server only.
Check out whether server is running in background.
We can start the server manually.
Saturday, April 8, 2023 Dept Of ISE, PESIT 3
MySQL/etc/init.d/mysqld start
Now you can able to connect as
$ mysql –u root
You will get mysql prompt
Mysql> show databases;
Mysql> CREATE DATABASES;
Case insensitive
Usually we use uppercase for key words.
Database is container for tables
Table is collection of rows
Row is collection of fields
Saturday, April 8, 2023 Dept Of ISE, PESIT 4
Mysql
Mysql> USE database-name;
Mysql> CREATE TABLE studinfo(
lastname CHAR(20),
firstname CHAR(20),
age INT);
DROP table name;
Ls –l /var/lib/mysql
We get all database directories
Ls –l /var/lib/mysql/people
We get table files in that directories
Saturday, April 8, 2023 Dept Of ISE, PESIT 5
Mysql Data types1. TINYINT -128 to 127(signed)
or 0- 255(unsigned)
2. SMALLINT -32768 to 32767(signed) or
o to 65535(unsigned)
3.MEDIUMINT -8388608 to 8388607 or
0 to 16777215
4. INT or INTEGER -2147483648 to 214783647 or 0 to 4294967295
5. BIGINT = 0 to 18446744073709551615 or -9223372036854775808 to 9223372036854775807
Saturday, April 8, 2023 Dept Of ISE, PESIT 6
MySQL data types
FLOAT
Double
REAL(same as double)
DECIMAL
NUMERIC(same as decimal)
Saturday, April 8, 2023 Dept Of ISE, PESIT 7
MySQL data types for DATE
Date YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MM;SS
TIMESTAMP YYYYMMDDHHMMSS, YYMMDDHHMMSS
TIME HH:MM:SS
YEAR YYYY or YY
Saturday, April 8, 2023 Dept Of ISE, PESIT 8
Other character data typesHave BLOB– Binary Large Object that could hold a variable amount of data.
BLOBs are case sensitive.
VARCHAR variable length string upto 255 chars
TINYBLOB – Maximum length 255 chars
TEXT
BLOB max. length 65535 characters
MEDIUMBLOB OR MEDIUMTEXT –max. 16777215 characters
LONGBLOB OR LONGTEXT – 4294967295 characters
Saturday, April 8, 2023 Dept Of ISE, PESIT 9
CommandsDescribe command gives the information about the table.
Describe book;
Insert inserts the values into table.
Insert into book values(‘ ‘,’ ‘);
Insert into book(name, usn) values(‘hhh’,’777’);
Select selects the records.
Select * from book order by name;
Select * from book order by name desc;
Saturday, April 8, 2023 Dept Of ISE, PESIT 10
CommandsUpdate –to change the value of an existing record.
Update book set usn=‘777’ where name=“ccc”;
Update book set sal=sal+10777 where name=“ccc”;
Delete command is used to delete the record from table.
Saturday, April 8, 2023 Dept Of ISE, PESIT 11
Some administrative detailsRoot is MySQL user.
Change user name to something
This can be done changing the databases and granting the previleges to new user.
Use mysql(existing databases)
Grant select, delete, update, insert on people.* to apache@localhost identified by ‘password’
Here apache is user
Password is password.
People is new database.
Saturday, April 8, 2023 Dept Of ISE, PESIT 12
Setting password is necessary for the first time\
Now use mysql server as follows
$mysql –u apache –p
Enter the password next.
Change the database using USE people.
Show tables;
Saturday, April 8, 2023 Dept Of ISE, PESIT 13
DATABASE INDEPENDENT INTERFACEDBI enables one to write programs to automate database maintenance & to write other scripts to interface with MySQLDBI is perl module that provides methods to manipulate SQL databases.With DBI one can connect to a database within a perl script & issue all kinds of queries, including SELECT, INSERT, DELETE.PERL program can be to written to connect to database.CGI script can be written to conect and display database on the browser.
Saturday, April 8, 2023 Dept Of ISE, PESIT 14
Simple perl program#!/usr/bin/perl –w
use strict;
use DBI;
my $dbh=DBI->
connect(‘DBI:mysql:people’,’apache’,’password’) or die “cannot connect “ . DBI->errstr() ;
print “success connected”;
$dbh->disconnect();
Saturday, April 8, 2023 Dept Of ISE, PESIT 15
Use DBI method tells perl to use the DBI module. This allows to use all methods for sql queries in this class.
Connect - causes perl to connect to the MySQL database using the perl DBI class.
The first argument to connect method is the database to which you want to connect.
Saturday, April 8, 2023 Dept Of ISE, PESIT 16
Ex. DBI:mysql:people
DBI module server name
database
‘apache’ – user
‘password’ – password.
If sucsessful returns database handler.
-disconnect to shut down the connection.
Saturday, April 8, 2023 Dept Of ISE, PESIT 17
#! /usr/bin/perl –w
use DBI;
use strict;
my $d;
$d=DBI->connect(‘DBI:mysql:people’,
’apache’,’password’) or die “cannot connect”.DBI->errstr();
my $sh=$d->prepare(‘SELECT * from book’) or die “can’t prepare”.
$d->errstr();
$sh->execute() or die “can’t execute”.
$sh->errstr();
Saturday, April 8, 2023 Dept Of ISE, PESIT 18
my($x,$y,$z);
While(($x,$y,$z)=$sh->fetchrow())
{
Print “$x $y $z \n”;
}
$sh->finish();
$d->disconnect();
Saturday, April 8, 2023 Dept Of ISE, PESIT 19
Prepare method prepares the statement & returns a statement handle object that can be used to execute the query by calling execute method.
One more perl example …
Saturday, April 8, 2023 Dept Of ISE, PESIT 20
#!/usr/bin/perl –w
use strict; use DBI;
my
$dbh=DBI->connect(‘DBI:mysql:people’,
’apache’,’password’) or die “cannot connect “ . DBI->errstr() ;
print “success connected”;
Print “\n”,”-”x40;
Print “enter name=“; Chomp($x=<STDIN>);
Print “enter address=“;Chomp($y=<STDIN>);
Print “enter author=“; Chomp($z=<STDIN>);
Saturday, April 8, 2023 Dept Of ISE, PESIT 21
my $sh=$d->prepare(‘INSERT INTO book(fname,lname,auth) values(?,?,?)’) or die “can’t prepare”.
$d->errstr();
$sh->execute($x,$y,$z) or die “can’t execute”. $sh->errstr();
Print “\n”,”-”x40;
Print “record inserted”;
$sh->finish();
$d->disconnect();
Saturday, April 8, 2023 Dept Of ISE, PESIT 22
Table Joins
Two or more tables can be joined to complete data.
Ex. Tables 1. book 2. address.
SELECT address.city FROM address, book where book.auth =“LAMP” AND
address.name=book.name;
Saturday, April 8, 2023 Dept Of ISE, PESIT 23
Select address.name, address.zip form address, book where address.age<=50 and address.name=book.auth and address.lanme=book.lname order by book.auth;
Saturday, April 8, 2023 Dept Of ISE, PESIT 24
Loading and dumping databasesCan load a database or otherwise
execute SQL commands from a file.
Simply put the commands or databases into a file ex. Mystuff.sql and load it in with this command.
$ mysql people < mystuff.sql
We can also dump out a database into a file with this command.
$mysqldump people < entiredb.sql;
Saturday, April 8, 2023 Dept Of ISE, PESIT 25