Mysql DBI

1,533 views

Published on

DBI MySql Apache PERL

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
1,533
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
15
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Mysql DBI

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

×