Jump to content

Recommended Posts

I've been trying to load a table from a 450MB .csv file but keep getting syntax errors. The insert sql looks like this:

LOAD DATA LOCAL INFILE '/home/larry/web/test/public_html/dbip-city-lite-2020-04.csv' INTO TABLE IP_LOOKUP FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

The code was copied from [https://www.mysqltutorial.org/import-csv-file-mysql-table/]

Does anyone see where I've gone wrong?

Additional question: How to accomplish this import on my web host?

Thanks in advance,
Larry

Link to post
Share on other sites

Got it working but having a problem getting the data into the fields in the proper format. Here's the sql I used to get it working:

LOAD DATA LOCAL INFILE  '/home/larry/web/test/public_html/dbip-city-lite-2020-04.csv'
INTO TABLE ip_lookup
FIELDS TERMINATED BY ',' ;

 

Here's the structure of the table:

CREATE TABLE `foxclone`.`ip_lookup` ( `id` INT(10) NOT NULL AUTO_INCREMENT , `start_ip` VARCHAR(20) NOT NULL , `end_ip` VARCHAR(20) NOT NULL , `continent` VARCHAR(2) NOT NULL , `country` VARCHAR(2) NOT NULL , `area` VARCHAR(50) NOT NULL , `city` VARCHAR(50) NOT NULL , `latitude` DECIMAL NOT NULL , `longitude` DECIMAL NOT NULL , PRIMARY KEY (`id`(50))) ENGINE = InnoDB;

Here's sample data from the csv:

105.155.190.0,105.155.191.67,AF,MA,Rabat-Salé-Kénitra,Rabat,34.015,-6.83272
105.155.191.68,105.155.191.255,AF,MA,Marrakesh-Safi,Marrakesh,31.6342,-7.99994
105.155.192.0,105.155.192.255,AF,MA,Fès-Meknès,Meknes,33.873,-5.54073
105.155.193.0,105.155.193.255,AF,MA,Oriental,Berkane,34.92,-2.32
105.155.194.0,105.155.194.255,AF,MA,Rabat-Salé-Kénitra,Rabat,34.015,-6.83272
105.155.195.0,105.155.195.255,AF,MA,Oriental,Oujda,34.6814,-1.90858

The id column isn't autoincrementing, the start_ip  is showing the wrong format (2a00:10:a9ff:ffff:ff) and the rest of the fields are in the wrong columns. 

 

Edited by larry29936
Link to post
Share on other sites

Unless you tell it otherwise, it assumes the csv contains the same fields as the table in the same order. You are not providing data for all the table columns so tell it which columns are present.

LOAD DATA LOCAL INFILE  '/home/larry/web/test/public_html/dbip-city-lite-2020-04.csv'
INTO TABLE ip_lookup
FIELDS TERMINATED BY ',' 
(start_ip, end_ip, continent, country, area, city, latitude, longitude)
;

 

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.