Perl Mysql database operation implementation code

Source: Internet
Author: User

1. Install the DBI Module
Step 1:
Download dbi.zip from the toolsbar and use winzip to unbind it to a temp directory. There are three files in total:
Readme
DBI. ppd
DBI.tar.gz
Step 2:
In the DOS window, run the following doscommand In the temp directory:
Ppm install DBI. ppd
If an invalid command is prompted, run it in the perl/bin directory.
2. Install the DBD-Mysql Module
Download dbd-mysql.zip from the software. The installation method is the same.
3. Prepare the database
Start mysql, first create a database mydata, and then create a table address
Mysql> create database mydata;
Query OK, 1 row affected (0.00 sec)
Mysql> use mydata;
Database changed
Mysql> create table address (
-> Id int (5) not null,
-> Name varchar (40) not null,
-> Email varchar (50) not null,
-> Telephone int (12) null );
Query OK, 0 rows affected (0.05 sec)
Enter some data:
Mysql> insert into address values (
-> 1, 'ghthawk', 'ghthawk @ 163.net', 92384092 );
Query OK, 1 row affected (0.00 sec)
4. Use the perl program to insert several records and perform query.
Use DBI;
# Connecting to the database mydata
My $ dbh = DBI-> connect ('dbi: mysql: mydata') or die "cannot connect to the database:". DBI-> errstr;
Print "insert several records \ n ";
My $ something = $ dbh-> prepare (q {
Insert into address (id, name, email, telephone) VALUES (?, ?, ?, ?)
})});
Print "input record, press enter to finish :";
While ($ inputdata = <> ){
Chop $ inputdata;
Last unless ($ inputdata );
My ($ id, $ name, $ email, $ tel) = split (/,/, $ inputdata );
$ Something-> execute ($ id, $ name, $ email, $ tel)
}
# $ Dbh-> commit;
Print "the EMAIL address and phone number \ n are printed based on the input name ";
My $ something = $ dbh-> prepare ('select * FROM address WHERE name =? ')
Or die $ dbh-> errstr;
Print "enter your name and press enter to finish :";
While ($ inputname = <> ){
My @ data;
Chomp $ inputname;
Last unless ($ inputname );
$ Something-> execute ($ inputname) or die "error:". $ something-> errstr;
While (@ data = $ something-> fetchrow_array ()){
Print "Email: $ data [2] \ t Telephone: $ data [3] \ n ";
}
}
# Disconnect
$ Dbh-> disconnect;
Nighthawk

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.