Importing CSV into MySQL with White Space - mysql

Anyone have any success importing a csv file into MySQL?
Here's an example csv file:
Full name,Source - Name
John,Youtube
Jon,FB
Jacob,Twitter
Here's the code:
CREATE TABLE Person
(ID INT AUTO_INCREMENT primary key,
fullname CHAR not null,
sourcenam1 CHAR,
);
LOAD DATA LOCAL INFILE '/Users/...DDB.csv' INTO TABLE t1
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
(Full name, Source - Name)
But I get an error that says my syntax is wrong. Specifically, it is the following:
ERROR 1064 (42000): You have an error in your SQL syntax
I would like to not have to delete the column names to solve this problem.

The names in the LOAD DATA column list should be the column names in the table, not the headings in the CSV file.
You also need to use the IGNORE option to skip over the heading line.
LOAD DATA LOCAL INFILE '/Users/...DDB.csv' INTO TABLE t1
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
IGNORE 1 LINES
(fullname, sourcenam1)

Related

Error in saving csv to MySQL table due to NULL values

I have the data saved in my csv excel. The data have some null value in row and column. I want to save this data into my database in MySQL. But null value is causing problem in saving the data to MySQL. This is the query for creating the table -
create table student (
Std_ID int,
Roll_NO int,
First_Name varchar(10) NOT NULL,
Last_Name varchar(10),
Class int,
constraint test_student primary key (Std_ID)
);
...and it ran successfully. Now I want to save my data from csv to this table using the query -
load data infile 'C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\new.csv' into table student fields terminated by ',' lines terminated by '\n' ignore 1 lines;
...and is giving me the error msg -
ERROR 1366 (HY000): Incorrect integer value: '' for column 'XXX' at row X.
For the reference you can use this data.
The same can be find below.
LOAD DATA INFILE 'C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\new.csv'
INTO TABLE student
FIELDS TERMINATED BY ','
LINES TERMINATED by '\n'
IGNORE 1 LINES
-- specify columns, use variables for the columns where incorrect value may occur
(Std_ID, #Roll_NO, First_Name, Last_Name, #Class)
-- use preprocessing, replace empty string with NULL but save any other value
SET Roll_NO = NULLIF(#Roll_NO, ''),
Class = NULLIF(#Class, '');
If some column in CSV is empty string then NULL value will be inserted into according column of the table.
Std_ID is not preprocessed because it is defined as PRIMARY KEY, and it cannot be NULL.
UPDATE
OP provides source file sample. Viewing it in HEX mode shows that the file is Windows-style text file, and hence the lines terminator is '\r\n'. After according edition the file is imported successfuly.

mysql table formatting and infile issue

Each time I go and create my table and then add the .csv file, it keeps not fully loading in all of the data. Below is my code:
CREATE TABLE Parts_Maintenance (
Vehicle_ID BIGINT(20),
State VARCHAR(255),
Repair VARCHAR(255),
Reason VARCHAR(255),
Year YEAR,
Make VARCHAR(255),
Body_Type VARCHAR(255),
PRIMARY KEY (Vehicle_ID)
);
LOAD DATA INFILE '/home/codio/workspace/FleetMaintenanceRecords.csv'
INTO TABLE Parts_Maintenance
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\\n'
IGNORE 1 ROWS;
SELECT * FROM Parts_Maintenance;
Here is a photo of what it looks like in Codio:
And here is a photo of some of the data being brought in:
Could someone please help me pinpoint what I am doing wrong?
Tried to create table and bring in a .csv file. Table was created but the data is not all there and the table looks messed up
I agree with #barmer Your LINES TERMINATED BY is probably wrong, it's probably \r
You May use
LOAD DATA INFILE '/home/codio/workspace/FleetMaintenanceRecords.csv'
INTO TABLE Parts_Maintenance
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS;
Alternatively, you can use another method with workbench.
Right Click on Table
- Table data import wizard
- Choose the file path
Select the destination table
- Check and configure import settings
- Click on Next to execute.
This is the best and simple solution.

Loading a table from a csv file: Error code 1292 Incorrect DATA value, but it's correct

I have a csv file with dates written as YYYY-MM-DD but I'm getting an incorrect date value while importing the file. I created the table and tried to import a csv file as it as follows:
CREATE TABLE afiliados (
afiliado_id INT PRIMARY KEY,
fecha DATE,
nombre VARCHAR(30),
municipio VARCHAR(20),
dni_impreso VARCHAR(2),
ficha_bien_llenada VARCHAR(2),
comentario VARCHAR(100)
);
DESCRIBE afiliados;
LOAD DATA INFILE '/var/lib/mysql-files/fichis.csv'
INTO TABLE afiliados
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
And here is what the csv file looks like
When I execute the second part, i get this:
Error Code: 1292. Incorrect date value: '' for column 'fecha' at row 1
I'm following this tutorial
http://www.mysqltutorial.org/import-csv-file-mysql-table/
I'm not sure if it's the correct to use ENCLOSED BY "" or LINES TERMINATED BY '\n' but the guy in the tutorial had a pretty similar file to mine. Either way, erasing these terms yields the same result.
EDIT: Managed to solve it by importing the file using a wizard that MySQL Workbench has, but I'm still wondering how to do it manually.

Loading a file into MYSQL table error code 1062 Duplicate entry for primary key

The table is empty before load statement is executed
do a select * from code_protocol;
only entry is line with null values for all columns/fields
To test the error I created from excel by saving a comma delimited *.csv file
The csv file has only one entry/row and I am only loading primary key and one other column.
load data infile 'c:/wamp64/tmp/code_protocol1.csv'
into table code_protocol
FIELDS terminated by ','
enclosed by '"'
lines terminated by '\r\n'
ignore 1 rows
(protocol_id, protocol_disc
);
what am I doing wrong?
Please show your table design AND your csv. This is usually the case when defining a primary index w/o autoindex and the according column is not set in the CSV, MySQL is trying to add a null value for a primary index col then.

Import CSV to MySQL With Fewer Columns Than Destination Table

Is there a way to import a CSV into MySQL where the column numbers do not match?
I have a MySQL "Employees" table which we update once a month with a CSV file. I have added two additional columns to the "Employees" table that are relevant to our needs. Importing the table data, through MySQL Workbench, with the following SQL statement:
LOAD DATA LOCAL INFILE 'EmployeeDump-March.csv' REPLACE INTO TABLE employees
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
I receive the following error, which is absolutely correct... the column numbers don't match:
Error Code: 1261. Row 1 doesn't contain data for all columns 7.426 sec
The two extra fields have default values.
Is there a way to supress this error and have MySQL ignore those last two columns, leaving them alone?
You can (and should!) explicitly name the columns you are inserting into using LOAD DATA INFILE, just like a regular INSERT. You can also optionally set default values for the two new columns you added as part of the LOAD DATA INFILE statement.
Something like this. Just fill in the actual column names in the right order between the parens, and set the actual defaults and column names for the new columns if you don't want NULL:
LOAD DATA LOCAL INFILE 'EmployeeDump-March.csv' REPLACE INTO TABLE employees
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(old_column1, old_column2, ...)
set new_column1 = NULL,
new_column2 = NULL