将CSV导入phpmyadmin

30

我有一个长这样的CSV文件,

candidate_id,show_on_site,first_name,surname,gender,DOB,showdob,Location,height,eyes,hair_colour,hair_length,accents,unions,training,url,visible,availability
,26,urban talent,Strawberry,Shortcake,Female,11 Jan 1942,FALSE,Manchester,5'2,Brown,Black,Mid-length,Native Lancashire,Equity,Urban Talent TV & Drama Workshops,Strawberry-Shortcake---5.4.06.jpg,Yes,Yes
,29,urban talent,Rainbow,Brite,Female,12 Oct 1970,FALSE,Manchester,5'7,Brown,Dark Brown,Long,"Native Manchester, others include - Cheshire, RP, Patois, Standard USA",Equity Member,"BA Acting Studies, Arden School of Theatre<br>Urban Talent TV & Drama Workshops",Rainbow Brite 1_1.jpg,Yes,Yes
,31,urban talent,Webbigail,Vanderquack,Female,4 Jun 1947,FALSE,Manchester,5'0,Hazel,Blonde,Mid-length,"Native Manchester, others include - Liverpool, Cockney, Birmingham, West Country, Standard Scottish, Standard Welch, S Irish",,Manchester School of Acting<br>3 Years at David Johnson Acting Workshops,Webbigail Vanderquack web 1.jpg,Yes,Yes
,33,urban talent,Smurfette,Smurf,Female,1 Jul 1979,FALSE,Manchester,5'2,Dark Brown,Dark Brown,Long,"Native Manchester, others include - Liverpool, RP, Lancashire, Birmingham, Cockney, Devon, Geordie, West Country, Glasgow, Edinburgh, South African, Standard & Southern US, Persian, Asian, Indian ~ good ear for accents",,"Manchester School of Acting, with Mark Hudson<br>North Cheshire Theatre College, with David Johnson<Oldham Theatre Workshop",Smurfette Smurf web 4.jpg,Yes,Yes

我能否将这些数据插入到现有数据库的列中,而不是像我目前所做的那样将其作为新表插入,并添加列A、B、C、D、E等?

4个回答

46

在phpMyAdmin中,点击表格,然后点击页面顶部的“导入”选项卡。

浏览并打开csv文件。将字符集保留不变。除非你有一个巨大的数据集(或者服务器很慢),否则取消选择“部分导入”。在选择文件后,格式应该已经选为“CSV”,如果没有,则选择它(不使用LOAD DATA)。如果您想在导入之前清空整个表,请勾选“用文件替换表格数据”。如果您认为CSV文件中存在重复行,请选择“忽略重复行”。现在是重要的部分,将下面的四个字段设置为以下值:

Fields terminated by: ,
Fields enclosed by: “
Fields escaped by: \
Lines terminated by: auto

目前这些设置除了“Fields terminated by”(字段终止符)以外都是默认值,该项默认为分号。

现在点击“Go”按钮,它应该可以成功运行。


我不喜欢去除重复行的措辞。"忽略重复行"可以被解释为不对重复行进行任何操作(即忽略它们),这会导致它们被添加到您的表中。感谢您对它们实际执行的解释。 - Dennis
1
不得不将 Fields terminated by: , 更改为:Fields terminated by: ; - Ronnie Oosting
@Roonie 我也是!!不得不改成分号。 - hoover_D

13
在phpMyAdmin v.4.6.5.2中,有一个复选框选项"文件的第一行包含表列名....":

enter image description here


5

使用LOAD DATA INFILE SQL语句,您可以导入CSV文件,但无法更新数据。然而,有一个技巧可以使用。

  • Create another temporary table to use for the import
  • Load onto this table from the CSC

    LOAD DATA LOCAL INFILE '/file.csv'
    INTO TABLE temp_table
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY '\n'
    (field1, field2, field3); 
    
  • UPDATE the real table joining the table

    UPDATE maintable
    INNER JOIN temp_table A USING (field1)
    SET maintable.field1 = temp_table.field1
    

1
这是由于缺少自增字段ID导致的。如果您在文本编辑器中编辑并为ID字段添加逗号,则问题将得到解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接