简体   繁体   English

将CSV文件导入数据库MySQL

[英]Importing a csv file into database mysql

Well my question seems simple 好吧,我的问题似乎很简单

I have my csv file located in /etc/myData.csv 我的csv文件位于/etc/myData.csv中

I have my code 我有我的密码

USE assessment; 

DROP TABLE IF EXISTS test_data; 
CREATE TABLE test_data (
    ProductID int,
    ProductName varchar(255)
);

mysqlimport assessment  '/src/myData.csv';

SELECT * FROM test_data;

I tried msqlimport but it gives me syntax error. 我尝试了msqlimport但它给了我语法错误。

I even tried this command 我什至尝试了这个命令

mysqlimport  --fields-terminated-by=, assessment  /src/test_data.csv ;

Again syntax error. 再次是语法错误。 What's wrong with it? 它出什么问题了?

FYI: I already know LOAD command but I want to know other mysql command to import data. 仅供参考:我已经知道LOAD命令,但是我想知道其他mysql命令来导入数据。

mysqlimport is intended to be run from the shell command line nor via the SQL interface. mysqlimport旨在从shell命令行或通过SQL接口运行。

If you have an SQL interface you can use load data infile 如果您有SQL接口,则可以使用加载数据文件

 LOAD DATA LOCAL INFILE '/src/myData.csv' INTO TABLE test_data;

But you need to make sure the file is readable by the dbms uid. 但是您需要确保dbms uid可以读取该文件。

I assume you know that you need to run mysqlimport -command from command line. 我假设您知道您需要从命令行运行mysqlimport -command。 It's not a SQL command that you can run with in your other SQL commands. 这不是您可以在其他SQL命令中运行的SQL命令。 Actually mysqlimport is just a command line interface to LOAD INFILE so you should use that. 实际上,mysqlimport只是LOAD INFILE的命令行界面,因此您应该使用它。

If you want to use mysqlimport following command should work. 如果要使用mysqlimport,则以下命令应该起作用。 Before running the command, you should change the file name to test_data.csv to get the data to test_data table. 在运行命令之前,应将文件名更改为test_data.csv,以将数据获取到test_data表。

mysqlimport -u root -ppassword --local assessment '/src/test_data.csv' mysqlimport -u root -ppassword-本地评估'/src/test_data.csv'

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

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