问题
When I type in the code into the Terminal it creates the Database but doesn't create the Table. When I type in use "locations" then "Show TABLES" it tells me that no Tables were created. This code should create a Database and the Table then import the the csv file into the database "locations"
mysql -u root --password=password -e
"CREATE DATABASE locations;"
"CREATE TABLE location_T (number1 INT NOT NULL,
number2 INT NOT NULL,
number3 INT NOT NULL,
names VARCHAR(100) NOT NULL,PRIMARY KEY (number1));"
LOAD DATA LOCAL INFILE 'locations.csv'
INTO TABLE overview FIELDS TERMINATED by '\t'
ENCLOSED BY '"'LINES TERMINATED BY '\n' "(number1,number2,number3,names)"
I have even tried using the absolute path to the file.
This is the csv file
number1 number2 number3 names
50 15 16 john
45 20 40 lauren
23 90 22 james
46 21 16 jonathan
Does anyone know what I am doing wrong? I am using csh.
回答1:
Here is a full answer, based off of @Honeyboy's comment.
In addition to needing to USE locations;
there are some other things to fix:
- Shell quoting wasn't really quite right. For one thing, your existing code doesn't account for the line breaks -- it needs line continuation escapes to run as written.
- The name of the table changed between the
CREATE TABLE
statement and theLOAD DATA
statement - The
LOAD DATA
statement didn't ignore the header row of your tab delimited file.
mysql -u root --password=password -e \
'CREATE DATABASE locations; \
USE locations; \
CREATE TABLE location_T (number1 INT NOT NULL, \
number2 INT NOT NULL, \
number3 INT NOT NULL, \
names VARCHAR(100) NOT NULL,PRIMARY KEY (number1)); \
LOAD DATA LOCAL INFILE "locations.csv" \
INTO TABLE location_T FIELDS TERMINATED by "\t" \
ENCLOSED BY "\"" LINES TERMINATED BY "\n" IGNORE 1 LINES \
(number1,number2,number3,names)'
来源:https://stackoverflow.com/questions/58770622/problems-creating-tables-from-command-line-mysql