Perl Operations MySQL Database simple tutorial

Source: Internet
Author: User
Tags prepare mysql database perl script

Assume that the Perl language and MySQL database are installed.

DBI Installation: DBI Detailed information reference: http://dbi.perl.org/
1. Download DBI Package:

wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.620.tar.gz

2 Decompression

Tar xzf dbi-1.620.tar.gz

3 into the unpack package,

Perl makefile.pl

4 Make Test

5 Make

6 make install (if not root user, sudo)

DBD Installation:

1. Download DBD

wget http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.021.tar.gz

2.3. Ditto

In the third step is likely to appear mysql_config can not find the situation, should be not installed development kit

sudo apt-get install Libmysqld-dev

sudo apt-get install Libmysqlclient-dev

To install the two packages.

The rest is ditto.

After the installation, you can use the Perl script to do the operation of MySQL data.

Write the Perl scripting operations database

The code is as follows Copy Code

#! /usr/bin/perl

Use DBI;
My $driver = "Dbi:mysql";
My $database = "perl_test";
My $user = "root";
My $host = "localhost";
My $passwd = "root";

My $rules = "alert_rules";

My $dbh = Dbi->connect ("$driver:d atabase= $database; host= $host; user= $user;p assword= $passwd")
Or die "Can ' t connect:". dbi->errstr;



My $sth = $dbh->prepare ("Select App_name,receivers from $rules");


$sth->execute () or die "Can" T prepare SQL statement. $sth->errstr;

My $sth = $dbh->prepare ("Select App_name,receivers from $rules");


$sth->execute () or die "Can" T prepare SQL statement. $sth->errstr;
# Print the Acquired data
while (@recs = $sth->fetchrow_array) {
Print $recs [0]. ":". $recs [1]. " n ";
}


$sth->finish ();
$DBH->disconnect ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.