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;