Use dbi to connect to the mysql database using perl. By using DBI, Perl can easily connect to the mysql database: the code is as follows: Copy the code as follows :#! BinperluseDBI; # ConnecttotargetDBmy $ dbhDBI-connect (by using DBI, DB can easily connect to the mysql database using Perl:
The code is as follows:
The code is as follows:
#! /Bin/perl
Use DBI;
# Connect to target DB
My $ dbh = DBI-> connect ("DBI: mysql: database = eygle; host = localhost", "username", "password ", {'raiseerror' => 1 });
# Insert one row
My $ rows = $ dbh-> do ("insert into test (id, name) VALUES (1, 'eygl ')");
# Query
My $ sqr = $ dbh-> prepare ("SELECT name FROM test ");
$ Sqr-> execute ();
While (my $ ref = $ sqr-> fetchrow_hashref ()){
Print "$ ref-> {'name'} n ";
}
$ Dbh-> disconnect ();
Execution result:
The code is as follows:
[Root @ eygle ~] # Perl test. cgi
Eygle
In the future, many statistics can be directly written to the Mysql database through Perl, which is much easier to operate.
The pipeline code is as follows :#! /Bin/perl use DBI; # Connect to target DB my $ dbh = DBI-connect ("DB...