Some Suggestions on using Perl DBI for MySQL operations, perldbi

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.