This article describes how to use perl to connect to a mysql database through dbi. For more information about how to use DBI, use Perl to easily connect to a mysql database:
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.