csv - Loading Coma Separated Text files to MySQL: doesn't work correctly -
i'm trying load gtfs files (comma separated text files) mysql database. mysql workbench import wizard doesn't work. copied agency.txt
file agency.csv
, select wizard. went through steps; green checkmark said '0 records imported' @ end. no errors, no warnings. use mysql shell. file:
>>head -2 agency.csv agency_name,agency_url,agency_timezone,agency_lang,agency_phone,agency_fare_url chicago transit authority,http://transitchicago.com,america/chicago,en,1-888-yourcta,http://www.transitchicago.com/travel_information/fares/default.aspx
this sql script create table , load data:
create table agency ( agency_name varchar(255) character set utf8, agency_id varchar(255) character set utf8, agency_url varchar(255) character set utf8, agency_timezone varchar(255) character set utf8 ); load data local infile '/cta/agency.csv' table agency fields terminated ',' lines terminated '\n' ignore 1 lines (agency_name,@vagency_id,agency_url,agency_timezone) set agency_id = if(agency_id null, agency_name, @vagency_id);
and loaded table:
mysql> select * agency; +---------------------------+---------------------------+----------------------------+-----------------+ | agency_name | agency_id | agency_url | agency_timezone | +---------------------------+---------------------------+----------------------------+-----------------+ | chicago transit authority | chicago transit authority | america/chicago | en |
note agency_url in table america/chicago (timezone not url).
i not experienced in sql technologies think shouldn't hard put data database. can tell me 1. why happening? 2. how fix it?
you referring agency_id
column in set
clause before actual value has been set. hence, null
causing second column assigned value of first column. change code this:
set agency_id = if(@vagency_id null, agency_name, @vagency_id); ^^^ don't use agency_id here
full code:
load data local infile '/cta/agency.csv' table agency fields terminated ',' lines terminated '\n' ignore 1 lines (agency_name, @vagency_id, agency_url, agency_timezone) set agency_id = if(@vagency_id null, agency_name, @vagency_id);
Comments
Post a Comment