MYSQL import data from csv using LOAD DATA INFILE

前端 未结 11 1422
感动是毒
感动是毒 2020-11-22 09:12

I am importing some data of 20000 rows from a CSV file into Mysql.

Columns in the CSV are in a different order than MySQL table\'s columns. How to automatically assi

相关标签:
11条回答
  • 2020-11-22 09:45

    Before importing the file, you must need to prepare the following:

    • A database table to which the data from the file will be imported.
    • A CSV file with data that matches with the number of columns of the table and the type of data in each column.
    • The account, which connects to the MySQL database server, has FILE and INSERT privileges.

    Suppose we have following table :

    CREATE TABLE USING FOLLOWING QUERY :

    CREATE TABLE IF NOT EXISTS `survey` (
      `projectId` bigint(20) NOT NULL,
      `surveyId` bigint(20) NOT NULL,
      `views` bigint(20) NOT NULL,
      `dateTime` datetime NOT NULL
    );
    

    YOUR CSV FILE MUST BE PROPERLY FORMATTED FOR EXAMPLE SEE FOLLOWING ATTACHED IMAGE :

    If every thing is fine.. Please execute following query to LOAD DATA FROM CSV FILE :

    NOTE : Please add absolute path of your CSV file

    LOAD DATA INFILE '/var/www/csv/data.csv' 
    INTO TABLE survey 
    FIELDS TERMINATED BY ',' 
    ENCLOSED BY '"'
    LINES TERMINATED BY '\r\n'
    IGNORE 1 LINES;
    

    If everything has done. you have exported data from CSV to table successfully

    0 讨论(0)
  • 2020-11-22 09:48

    I was getting Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

    This worked for me on windows 8.1 64 bit using wampserver 3.0.6 64bit.

    Edited my.ini file from C:\wamp64\bin\mysql\mysql5.7.14

    Delete entry secure_file_priv c:\wamp64\tmp\ (or whatever dir you have here)

    Stopped everything -exit wamp etc.- and restarted everything; then punt my cvs file on C:\wamp64\bin\mysql\mysql5.7.14\data\u242349266_recur (the last dir being my database name)

    executed LOAD DATA INFILE 'myfile.csv'

    INTO TABLE alumnos

    FIELDS TERMINATED BY ','

    ENCLOSED BY '"'

    LINES TERMINATED BY '\r\n'

    IGNORE 1 LINES

    ... and VOILA!!!

    0 讨论(0)
  • 2020-11-22 09:51

    You can load data from a csv or text file. If you have a text file with records from a table, you can load those records within the table. For example if you have a text file, where each row is a record with the values for each column, you can load the records this way.

    table.sql

    id //field 1
    
    name //field2
    

    table.txt

    1,peter
    
    2,daniel
    
    ...
    

    --example on windows

    LOAD DATA LOCAL INFILE 'C:\\directory_example\\table.txt'
    INTO TABLE Table
    CHARACTER SET UTF8
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY '\r\n'; 
    
    0 讨论(0)
  • 2020-11-22 09:58

    let suppose you are using xampp and phpmyadmin

    you have file name 'ratings.txt' table name 'ratings' and database name 'movies'

    if your xampp is installed in "C:\xampp\"

    copy your "ratings.txt" file in "C:\xampp\mysql\data\movies" folder

    LOAD DATA INFILE 'ratings.txt' INTO TABLE ratings FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
    

    Hope this can help you to omit your error if you are doing this on localhost

    0 讨论(0)
  • 2020-11-22 09:59

    Syntax:

    LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL]
    INFILE 'file_name' INTO TABLE `tbl_name`
    CHARACTER SET [CHARACTER SET charset_name]
    FIELDS [{FIELDS | COLUMNS}[TERMINATED BY 'string']] 
    [LINES[TERMINATED BY 'string']] 
    [IGNORE number {LINES | ROWS}]
    

    See this Example:

    LOAD DATA LOCAL INFILE
    'E:\\wamp\\tmp\\customer.csv' INTO TABLE `customer`
    CHARACTER SET 'utf8'
    FIELDS TERMINATED BY ',' ENCLOSED BY '"'
    LINES TERMINATED BY '\r\n'
    IGNORE 1 LINES;
    
    0 讨论(0)
提交回复
热议问题