English 中文(简体)
将CSV导入MySQL-已建立字段
原标题:Import CSV into MySQL - With Fields Already Established

这可能是一个愚蠢的问题,但我似乎无法在谷歌上找到答案,也无法通过编程创建答案。我有一个名为contacts2.CSV(逗号分隔)的巨大CSV文件,我想将其导入MySQL数据库。现在棘手的部分是,我已经在数据库中有一堆字段,我想将数据放入其中,但它们与CSV不匹配。

例如,CSV上的第一列可能是“公司”,但实际上是MySQL上的第五列。我还有一个自动递增的“id”列,当前为129483653,我想在导入这些列时保持自动递增。

我在网上找到了一些脚本来显示数据并导入到新表中,但我需要在不覆盖任何内容的情况下将所有这些记录添加到现有表中。

非常感谢。

我使用了一个CSV到MySQL的转换器,并得到了以下输出:

CREATE TABLE `tempcontacts` ( 
`company` varchar(255) NOT NULL DEFAULT   , 
`contact` varchar(255) NOT NULL DEFAULT   , 
`address_1` varchar(255) NOT NULL DEFAULT   , 
`city` varchar(255) NOT NULL DEFAULT   , 
`state` varchar(255) NOT NULL DEFAULT   , 
`zip` varchar(255) NOT NULL DEFAULT   , 
`phone` varchar(255) NOT NULL DEFAULT   , 
`fax` varchar(255) NOT NULL DEFAULT   , 
`title` varchar(255) NOT NULL DEFAULT   , 
`phone_ext_` varchar(255) NOT NULL DEFAULT   , 
`web_site` varchar(255) NOT NULL DEFAULT   , 
`e_mail` varchar(255) NOT NULL DEFAULT   
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `tempcontacts` VALUES 
( Construction ,  Bill Billy ,  4201 State Route 1 ,  Orlando ,  FL ,  11111 ,  312-922-2125 ,   ,   ,  235 ,  www.construction.com ,   ), 

这难道不能以某种方式切换到名为“contacts”的实际表的“insert”命令吗?

更新:我想我可能需要有人帮我写一些定制的东西来完成这一切。我试过:

http://www.codewalkers.com/c/a/Database-Code/PHP-CSV-Importer-20/一

http://www.geeklog.net/forum/viewtopic.php?showtopic=71161一

除其他来源外。

最佳回答

I think your best bet would be reordering your mysql table to what your csv file order is. Then just reorder the mysql table in the order you want it. It s a mosh posh work around. even if you mess up the order everything is var(255) so you can just rename the column

编辑

As long as you have your rows in the correct order so that 1 is on line 1 and 2 is on line 2 etc. and your columns are in the correct order: Company, contact, address_1 etc. in your csv to match your table then you can just use a Data Infile script

http://dev.mysql.com/doc/refman/5.0/en/load-data.html一

一个我必须导入csv文件的示例:

LOAD DATA INFILE  /usr/name/file.csv  INTO TABLE `mytable`
COLUMNS TERMINATED BY  , 
OPTIONALLY ENCLOSED BY  " 
LINES TERMINATED BY  
 ;

然后,只需使用auto_increment添加列主键

I used this because someone had an excel sheet with a bunch of stuff that i needed to import to the database one time. I am still learning everything with databases so I don t know if there is a security risk if you made this a routine script.

编辑:

using one of the examples from http://dev.mysql.com/doc/refman/5.0/en/load-data.html一

LOAD DATA INFILE  #{DATA_FILE_NAME}  IGNORE
INTO TABLE zipcodes
FIELDS TERMINATED BY  , 
ENCLOSED BY  " 
LINES TERMINATED BY  \n 
(city, state_code, @zip, area_code, county_fips, county_name, @preferred, timezone, 
dst, lat, lon, msa, pmsa, @city_abbreviation, ma, zip_type)

To do this though, all your lines (rows) in your CSV file should be following the same pattern. So say: order of company, ownerid, name, title, address, then a hard enter ( )

所以它应该看起来像

PharmacyPlace, 2222, Bob Pills, Pharmacists Awesome Man, 51 Main st
Walmart, 12331, Anna Smith, Manager, 99 main st

保持相同的模式

If you have a line that is any other way then you going to have to fix that but if you have:

Taco Bell,,,Manager, 59 Main st

然后,每个逗号只会告诉它转到下一列。

所以这个例子上传到数据库的脚本是

LOAD DATA INFILE  DATA_FILE_NAME  INTO TABLE `your table`
FIELDS TERMINATED BY  , 
LINES TERMINATED BY  
 
(company, ownerid, name, title, address);

我会先备份你的表,然后将其复制到另一个表作为临时表来测试这一切。你将不得不经历一些试错。就像我说的,一个最豪华的解决方案。

问题回答

一个每周走这条路十几次的人的话。导入到临时表并使用SQL引擎或PHP脚本从中找出它的优点远远超过了试图匹配/映射和希望的痛苦。之前已经回答过这个问题这里。只是我的意见,我可能错了。

从您编辑的问题开始:该转换器生成了一个INSERT语句,您可以对其进行类似的修改。将表更改为最终表,并将显示的列名调整为最终表中的实际列,但不更改其顺序(列名必须与下面的:data顺序相同,但不必与表结构中的顺序相同)

INSERT INTO `contacts` 
(`company`, `contact`, `address_1`, `city`, `state`, `zip`, `phone`, `fax`, `title`,     `phone_ext_`, `web_site`, `e_mail`) 
VALUES
( Construction1 , Bill Billy , 4201 State Route 1 , Orlando , FL , 11111 , 312-922-2125 ,  ,  , 235 , www.construction.com ,  ),
( Construction2 , Bill Billy , 4201 State Route 1 , Orlando , FL , 11111 , 312-922-2125 ,  ,  , 235 , www.construction.com ,  );

;





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签