Some Suggestions on using Perl DBI for MySQL operations, perldbi
Using perl to connect to mysql, there are many cases on the Internet. Generally, we use the DBD: MySQL module under DBI. Here we create a mask to get a TIPS:
Perl DBI MySQL Character Set: UTF8
An error is reported when Perl DBI special characters are written.
Perl DBI connection automatic reconnection or connection timeout
1. When the MySQL character set is UTF8, You need to introduce:
use utf8;binmode(STDOUT, ':encoding(utf8)');binmode(STDIN, ':encoding(utf8)');binmode(STDERR, ':encoding(utf8)');
Purpose:
Solve the problem that the display result is garbled after perl connects mysql to the data.
2. For writing special characters, it is best to use:
my $sth=$dbh->prepare("insert into wubx.WeekEvent values(?,?,?,?,?,?,?)"); $sth->execute($OId,$CId,qq/$Time/,$EventType,qq/$CDesc/,$PId,$RFlag);
The character string may be submitted by the user using qq // package, reduce the special characters, resulting in SQL cannot be executed.
3. If two databases are connected for exchange operations or data migration, the connection timeout should be considered.
Error: MySQL server has gone away
Solution:
After DBD: mysql 4.012 supports automatic reconnection for DBI connections. You need to set the following parameters:
$dbh->{mysql_auto_reconnect} = 1;
This is not supported in earlier modules. A simple method is as follows:
$dbh->do('set SESSION wait_timeout=72000'); $dbh->do('set SESSION interactive_timeout=72000');
This method is applicable to the short-term lost connection to MySQL in other languages or the timeout time of the Server is too short.