下面来看两个mysql 导入导出CSV数据的例子,这此都用到了mysql自带的命令了,如 LOAD DATA LOCAL INFILE 或select * INTO OUTFILE命令了.
cvs文件导入MySql数据库命令,代码如下:
set names utf8; LOAD DATA LOCAL INFILE 'C:\\resource.csv' INTO TABLE resource FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\r\n' (title,singer); --phpfensi.com FIELDS TERMINATED BY ---- 字段终止字符 OPTIONALLY ENCLOSED BY ---- 封套符 LINES TERMINATED BY ---- 行终止符 导出一段时间的数据到 cvs,代码如下:
#mysql里先导出数据到 xml, 用 select into outfile 导出,设定字段分隔符和记录分隔符.
/usr/local/webserver/mysql/bin/mysql -h "$mysql_host" -u "$mysql_username" --password="$mysql_password" -e "select * INTO OUTFILE '$data_dir$fileName' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n' from $table where InTime>=$minTime && InTime<$maxTime;"