视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
如何把excel数据导入到mysql数据库中_MySQL
2020-11-09 18:19:50 责编:小采
文档


bitsCN.com
如何把excel数据导入到mysql数据库中 最近做的工作涉及到把excel数据导入到mysql数据库中, 看来一些mysqlimport, phpmyadmin命令, 但是不怎么会用. 就决定自己写脚本解决. 先把excel数据文件保存成csv格式的文件, 然后写了下面的简单perl脚本倒入mysql数据库中.需要注意用你的mysql数据库表的结构替换掉. 运行脚本并且把csv格式的文件做为参数即可. #!/usr/bin/perl # 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: 2012-9-20 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_handler;sub __int_handler{ 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>) { 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 lines files. NO error found./n";exit(0);
bitsCN.com

下载本文
显示全文
专题