Import and export MySQL using DBI (perl) for text files

Source: Internet
Author: User
Tags perl script

DBI is a module of the Perl script connection database.
Perl scripts are more flexible, more powerful, and cross-platform than the shell. The relative executable jar package is much simpler.

? 1, download the installation package
Dbi-1.631.tar.gz
perl Script Download website http://www.cpan.org/
Many Perl components can be downloaded from this website.

2. Decompression
TAR-XZVF dbi-1.631.tar.gz
CD DBI-1.631
Perl makefile.pl
Make
Make Test

3. DBD Installation
Download dbd-mysql-4.032.tar.gz (address same 1)
CD dbd-mysql-4.032
Perl makefile.pl--mysql_config=/usr/local/mysql/bin/mysql_config
Make
Make Test
Make install

4. Other possible conditions
If you run a program that calls the MySQL database, you see the following system prompt:
Install_driver (MySQL) failed:can ' t load '/usr/lib/perl5/site_perl/5.6.1/i386-linux/auto/dbd/mysql/mysql.so ' for Module Dbd::mysql:/usr/lib/perl5/site_perl/5.6.1/i386-linux/auto/dbd/mysql/mysql.so:undefined symbol:uncompress AT/USR/LIB/PERL5/5.6.1/I386-LINUX/DYNALOADER.PM Line 206. At (Eval 2) Line 3
Then the DBD::MYSQL is not installed successfully.
This will/usr/local/mysql/include/mysql/*
Copy to/usr/include/
At the same time,/usr/local/mysql/lib/mysql/*
Copy to/usr/lib/(64-bit system/usr/lib64/)
Then use Perl makefile.pl to set up the environment, and then follow the normal installation, you can properly connect to the MySQL database.

Solutions for/** 32-bit systems
cp-rp/usr/local/mysql/include/mysql*/usr/include/
cp-rp/usr/local/mysql/lib/*/usr/lib/
**/
5. Import and Export script instance

Import
#!/usr/bin/perl
Use strict;
Use DBI;
My $dbh = Dbi->connect ("Dbi:mysql:database=test;host=localhost", "root", "admin", {' RaiseError ' = 1});
My $rows = $dbh->do ("LOAD DATA LOCAL INFILE ' test_data.txt ' into TABLE users fields TERMINATED by ' \ t ' optionally enclo SED by ' \ ' ' LINES TERMINATED by ' \ n ' ");
Print "$rows row (s) affected";

Reference
Http://www.cnblogs.com/ggjucheng/archive/2012/11/05/2755683.html

Export:
#!/usr/bin/perl
Use strict;
Use DBI;
My $dbh = Dbi->connect ("Dbi:mysql:database=test;host=localhost", "root", "admin", {' RaiseError ' = 1});
My $rows = $dbh->do ("SELECT * from users to outfile '/tmp/test_out_data.txt ' fields terminated by ' \ t ' enclosed by ' \ "' lines terminated by ' \ n '");
Print "$rows row (s) affected";
Reference:
Http://www.cnblogs.com/wxb-km/archive/2012/10/23/2736394.html

Import and export MySQL using DBI (perl) for text files

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.