How to import excel data to a mysql database

Source: Internet
Author: User
Tags perl script


Recently, how to import excel data to a mysql database involves importing excel data to a mysql database. It seems that some mysqlimport and phpmyadmin commands are not easy to use. I decided to write the script myself. first, save the excel data file as a csv file, and then write the following simple perl script into the mysql database. note that the structure of your mysql database table should be replaced. run the script and take the csv file as the parameter. #! /Usr/bin/perl www.2cto.com # perl program: insert_mysql_data.pl # function: insert mysql data from csv file # usage: insert_mysql_data.pl csv_file # Author: shenxiangfeng@360.cn # Date: use strict; use warnings; use DBI; # define the global varsmy $ CHECK_DEBUG = 1; my $ CSV_FILE; my $ lines = 0; # ------------------------ # signal handle function # ------------------------ local $ SIG {'int' }=\&__ int_ha Ndler; sub _ int_handler {www.2cto.com my @ int = @ _; if ($ int [0]) {# Called on a user interrupt die "\ nInterrupted by \" ctrl + c \ "\ n" ;}# get the parametermy $ parameter = shift; if (! -E $ parameter) {die ("program parameter is not right. \ nUsage: md5_check.pl package_name_or_directory \ n ");} elsif (-f $ parameter) {# connect mysql databasemy $ dbh = DBI-> connect (" DBI: mysql: database = tuiguang; host = localhost "," root "," ", {'raiseerror' => 1}); $ dbh-> do (" set names 'utf8 '"); # parameter is csv file open ($ CSV_FILE, "<", "$ parameter") or die "Can't open csv file $ parameter. \ n "; while (<$ CSV_FILE>) {www.2cto.com my $ line =$ _; chomp ($ line); my @ parts = split (/,/, $ line ); print "@ parts will insert. \ n "if ($ CHECK_DEBUG); my $ rows = $ dbh-> do (" insert into AccountInfo (ID, Name, PassWord, Property, ChanType, Status, Discount, ChanBelong, downloadUrl) values ('$ parts [0]', '$ parts [1]', '$ parts [2]', $ parts [3], $ parts [4], $ parts [5], $ parts [6], $ parts [7], '$ parts [8]') "); print" $ rows row (s) affected. \ n "if ($ CHECK_DEBUG); $ lines ++;} # clean up close ($ CSV_FILE); $ dbh-> disconnect ();} # post process print "Successfully process the $ lines files. NO error found. \ n "; exit (0 );
 

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.