• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Mysql DBI
 

Mysql DBI

on

  • 2,137 views

DBI MySql Apache PERL

DBI MySql Apache PERL

Statistics

Views

Total Views
2,137
Views on SlideShare
2,132
Embed Views
5

Actions

Likes
0
Downloads
12
Comments
1

2 Embeds 5

http://mahinthjoe.googlepages.com 4
http://www.slideshare.net 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Mysql DBI Mysql DBI Presentation Transcript

    • 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.
    • 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.
    • 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
    • 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
    • Mysql Data types
      • 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
    • MySQL data types
      • FLOAT
      • Double
      • REAL(same as double)
      • DECIMAL
      • NUMERIC(same as decimal)
    • 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
    • Other character data types
      • Have 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
    • Commands
      • Describe 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;
    • Commands
      • Update –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.
    • Some administrative details
      • Root 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 .
      • 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;
    • DATABASE INDEPENDENT INTERFACE
      • DBI enables one to write programs to automate database maintenance & to write other scripts to interface with MySQL
      • DBI 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.
    • 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();
      • 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.
      • Ex. DBI:mysql:people
      • DBI module server name
      • database
      • ‘ apache’ – user
      • ‘ password’ – password.
      • If sucsessful returns database handler.
      • -disconnect to shut down the connection.
      • #! /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();
      • my($x,$y,$z);
      • While(($x,$y,$z)=$sh->fetchrow())
      • {
      • Print “$x $y $z ”;
      • }
      • $sh->finish();
      • $d->disconnect();
      • 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 …
      • #!/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 “ ”,”-”x40;
      • Print “enter name=“; Chomp($x=<STDIN>);
      • Print “enter address=“;Chomp($y=<STDIN>);
      • Print “enter author=“; Chomp($z=<STDIN>);
      • 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 “ ”,”-”x40;
      • Print “record inserted”;
      • $sh->finish();
      • $d->disconnect();
    • 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;
      • 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;
    • Loading and dumping databases
      • Can 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;
    •