MySQL ignoring fields in a csv file after LOAD DATA statement - mysql

I am loading a local csv for a table in MySQL. The table has 6 columns (or fields), but, after I load the data, only the message_date column is filled. In other words, the other 5 columns in the table are all NULL, after the LOAD DATA run. Why? I need to SET all of the 5 columns to bring them to my table?
Detail: When I do not SET any variable, just import them all as character fields, all of the columns appears. So it looks like is something after the IGNORE statement that is affecting all of the other 5 columns.
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/DadosBrutosEventTracks.csv'
INTO TABLE messages
FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n'
IGNORE 1 LINES
(#var1, #var2, #var3, #var4, #var5, #var6)
SET
message_date = STR_TO_DATE(SUBSTRING(#var3, 1, 22), "%Y-%m-%d %H:%i:%s.%f");

You only load data into variables. Therefore no data goes into any columns. Loading data into variables happens instead of loading into columns.
(#var1, #var2, #var3, #var4, #var5, #var6)
Then you set one column:
SET
message_date = STR_TO_DATE(SUBSTRING(#var3, 1, 22), "%Y-%m-%d %H:%i:%s.%f");
The other columns are not mentioned in the SET clause, so they get no data.

Related

Insert string in MySQL in one column

(MySQL) I have a table with 2 columns.
Table 2 column is a varbinary column.
I have txt file that has 2040 binary strings converted to numbers ( ie 000001 = 000001 , 000100 = 000004 etc).
I am tring to find a statement to insert the data into table. i tried
"LOAD DATA LOCAL INFILE 'C:/ProjectFolder/MySQLHex/Hex.txt' INTO TABLE testbinary
LINES TERMINATED BY '\n'
(#col1,#col2) set representation=#col2; " .
It inserted all rows as NULL values.
If i manually insert 1 row (insert statment), it works !! How do i load the txt file into 2 column of the table using a command ?
You're missing a FIELDS TERMINATED BY '=' in your statement. Therefore the whole line is treated as one column.
It inserted all rows as NULL values, because you load the whole line from your txt file into variables, and just the second column (which is NULL because your whole line is in variable #col1) is set to the variable #col2.
Since you don't do any transformations or whatever with your variables, those are completely unnecessary in this case. Just insert directly into the columns, without using variables.

Load SQL infile with first column fixed width

I have a plain text file with data like this:
B01-CA This is first data
Z01 This is second data
A56-COL This is third data
So I want to insert these data in a table with 3 columns id, code, name.
I know the syntax:
load data local infile 'C:/codes.txt'
into table tbl_test
fields
terminated by ' '
enclosed by ' '
lines
terminated by '\n'
(code,name);
But how can I take first substring upto 8 charactrs in code column and rest line till '\n' to name column. id is auto increment. Any ideas will help.
I have had the same question.
I have done this:
load data local infile '<file>' into table <table_name> (#row) set code = trim(substr(#row, 0,7)), name = trim(substr(#row, 8))
it works for me

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

string to timestamp mysql Error #1411

I'm trying to convert timestamps on the fly when importing a csv file into mysql from string to datetime data type. But I am getting a #1411 - Incorrect datetime value: '2007-03-30 16:01:15' for function str_to_date error.
The SQL:
load data infile 'C:/ProgramData/MySQL/MySQL Server 5.5/data/testfile.csv'
into table test
fields terminated by ','
lines terminated by '\n'
(date, col1,col2,col3,col4)
SET
date = str_to_date(date,'%Y.%m.%d %H:%i:%s.%f');
All rows in the .csv are formated like this:
2007.03.30 16:01:15.901,117.53,117.55,35600000,43700000
I've applied
SELECT str_to_date(date,'%Y.%m.%d %H:%i:%s.%f') FROM test
to sample data that was already stored in mysql, it did work.
The target row date is set to DATETIME.
You need to go via a user variable. As the manual says:
The column list can contain either column names or user variables. With user variables, the SET clause enables you to perform transformations on their values before assigning the result to columns.
User variables in the SET clause can be used in several ways. The following example uses the first input column directly for the value of t1.column1, and assigns the second input column to a user variable that is subjected to a division operation before being used for the value of t1.column2:
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, #var1)
SET column2 = #var1/100;
In your case:
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 5.5/data/testfile.csv'
INTO TABLE test
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(#date, col1, col2, col3, col4)
SET date = STR_TO_DATE(#date, '%Y.%m.%d %H:%i:%s.%f');

mysql insert with auto-increment column

I am trying to insert data into a mysql table from a csv file. I am using the infile sql command, but I am having trouble because the first column of the table is an id that is set as an auto increment field. what do I have to set my first column value to in order to get this to work, or can I do it at all?
Thanks
Try using an empty field or a TAB character as a first column value in a file
(see comment "Posted by Mohamed Abdulla on August 17 2005 11:14am" on http://dev.mysql.com/doc/refman/5.0/en/loading-tables.html)
Another solution:
LOAD DATA INFILE 'DATA.txt' INTO TABLE your_table
FIELDS TERMINATED BY '^'
OPTIONALLY ENCLOSED BY '~'
(column2, column3, ... )
SET column1 = NULL