简体   繁体   English

使用脚本将.csv文件加载到MySQL数据库中

[英]Loading a .csv file into a MySQL database using a script

I am using the Ubuntu 10.04 LTS command line to run a mySQL 5.1.61 script that ought to take a .csv file and use its contents to fill a table I created earlier in the script. 我正在使用Ubuntu 10.04 LTS命令行运行mySQL 5.1.61脚本,该脚本应采用.csv文件,并使用其内容填充我在脚本中先前创建的表。

Here's what's in the script (the csv has three columns, first name, last name, and club): 这是脚本中的内容(csv具有三列,名字,姓氏和俱乐部):

-- Import club roster from csv.
-- Create a table to store club data.
CREATE TABLE club_roster (
    first_name VARCHAR(32),
    last_name VARCHAR(32),
    club VARCHAR(32)
)

followed by: 其次是:

-- Put information from csv into table.
LOAD DATA LOCAL INFILE '/complete/path/to/file.csv'
    INTO TABLE club_roster
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES;

When I attempt to run it, I get this error: 当我尝试运行它时,出现以下错误:

ERROR 1064 (42000) at line 17: You have an error in your SQL syntax; 第17行的错误1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LOAD DATA LOCAL INFILE '/complete/path/to/file.csv' INTO TABL' at line 8 检查与您的MySQL服务器版本对应的手册以获取正确的语法,以在第8行的'LOAD DATA LOCAL INFILE'/complete/path/to/file.csv'INTO TABL'附近使用

What about my syntax is incorrect? 我的语法不正确怎么办? I have tried every combination of paths to the file, including with single quotes, without single quotes, with the entire path, with just the file, with the file ending, without the file ending, and combinations of these. 我尝试了文件路径的每种组合,包括单引号,无单引号,整个路径,仅文件,文件结尾,文件末尾以及它们的组合。

Any ideas? 有任何想法吗? Thank you for the help! 感谢您的帮助!

EDIT: I am using the command 编辑:我正在使用命令

mysql < 'scriptFileName' -u root -p

and then entering my password to run the script. 然后输入我的密码来运行脚本。 Would the command matter? 命令重要吗?

You should terminate your create table command with a semi-colon. 您应该使用分号终止create table命令。 Like so: 像这样:

CREATE TABLE club_roster (
first_name VARCHAR(32),
last_name VARCHAR(32),
club VARCHAR(32)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM