larry29936 Posted April 26, 2020 Share Posted April 26, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310648-sql-insert-from-cli/ Share on other sites More sharing options...
Barand Posted April 26, 2020 Share Posted April 26, 2020 What error messages are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/310648-sql-insert-from-cli/#findComment-1577254 Share on other sites More sharing options...
larry29936 Posted April 26, 2020 Author Share Posted April 26, 2020 (edited) 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 April 26, 2020 by larry29936 Quote Link to comment https://forums.phpfreaks.com/topic/310648-sql-insert-from-cli/#findComment-1577258 Share on other sites More sharing options...
Barand Posted April 26, 2020 Share Posted April 26, 2020 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) ; Quote Link to comment https://forums.phpfreaks.com/topic/310648-sql-insert-from-cli/#findComment-1577265 Share on other sites More sharing options...
larry29936 Posted May 1, 2020 Author Share Posted May 1, 2020 @Barand, sorry for the delay in responding, life got in the way. Your solution worked, thank you. I have a new issue which I'll create a new post for. Quote Link to comment https://forums.phpfreaks.com/topic/310648-sql-insert-from-cli/#findComment-1577371 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.