Some suggestions for using Perl dbi to manipulate Mysql _mysql

Source: Internet
Author: User

Using Perl to connect to MySQL, there are many cases on the Internet, and generally we are dbi under the Dbd::mysql this module. Here make a mask get a tips:
Perl DBI MySQL's character set is UTF8
Perl DBI special characters write times wrong
Perl DBI Connection Automatic reconnection or connection timeout

1. When MySQL's character set is UTF8, it needs to be introduced:

Use UTF8;
Binmode (STDOUT, ': Encoding (UTF8) ');
Binmode (STDIN, ': Encoding (UTF8) ');
Binmode (STDERR, ': Encoding (UTF8) ');

Objective:
Resolve Perl connection MySQL to the data after reading the display result is garbled problem.

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);


For strings that may be submitted by the user with a qq//package, reducing the special characters causes SQL to not execute.
3. If you connect two databases with Exchange operations or move data, consider a timeout.
Error: MySQL server has gone away
Treatment methods:
The automatic reconnection of DBI connections is supported after Dbd::mysql 4.012. Need to set:

 $DBH->{mysql_auto_reconnect} = 1;

Not supported in earlier modules, simple method:

 $DBH->do (' Set session wait_timeout=72000 ');
 $DBH->do (' Set session interactive_timeout=72000 ');

This method is applicable in other languages even if the MySQL connection is short lost or the timeout time setting for 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.