How to access mysql database with perl
Editor to share with you how to use perl to access mysql database, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
one。 Install the DBI module
Step 1:
Download DBI.zip from the TOOLS column and unlock it into a temp directory with winzip after downloading. There are three files in total:
Readme
DBI.ppd
DBI.tar.gz
Step 2:
Under the DOS window, run the following DOS command in the temp directory:
Ppm install DBI.ppd
If you prompt an invalid command, you can run it in the perl/bin directory
two。 Install the DBD-Mysql module
Download DBD-Mysql.zip from the software download, the installation method is the same.
three。 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 "Nighthawk", "Nighthawk", "163.netdog", "92384092")
Query OK, 1 row affected (0.00 sec)
four。 Below, use the perl program to insert several records and make queries.
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 $sth = $dbh- > prepare (Q {
INSERT INTO address (id, name,email,telephone) VALUES (?)
})})
Print "enter record, enter end:"
While ($inputdata =) {
Chop $inputdata
Last unless ($inputdata)
My ($id, $name,$email, $tel) = split (/, /, $inputdata)
$sth- > execute ($id, $name, $email,$tel)
}
# $dbh- > commit
Print "print out the EMAIL address and phone number n according to the name entered below"
My $sth = $dbh- > prepare ('SELECT * FROM address WHERE name=?')
Or die $dbh- > errstr
Print "Please enter your name and press enter to finish:"
While ($inputname =) {
My @ data
Chomp $inputname
Last unless ($inputname)
Sth- > execute ($inputname) or die "error:". $sth- > errstr
While (@ data = $sth- > fetchrow_array ()) {
Print "Email:$data [2] t Telephone:$data [3] n"
}
}
# disconnect
$dbh- > disconnect
The above is all the contents of the article "how to access mysql database with perl". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!