Insert string in MySQL in one column - mysql

(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.

Related

MySQL ignoring fields in a csv file after LOAD DATA statement

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.

Insert column from *.txt file to end of existing MySQL table

I am trying to load the contents of a *.txt file to the end of a MySQL database table.i.e. I want to append the contents of the file to an existing table in MySQL. I am working in MySQL Workbench (Windows 7). I downloaded the Windows 5.6.21 installer from http://dev.mysql.com/downloads/windows/installer/:
Here is the MySQL code that I am working with:
Create database, table and load file:
CREATE SCHEMA fileimport2;
USE fileimport2;
CREATE TABLE testtable(column1 INT, column2 VARCHAR(255), column3 DECIMAL(8,4));
LOAD DATA INFILE 'File.txt'
INTO TABLE foo COLUMNS TERMINATED BY '\t';
At this point, the database has one table with 3 rows and 3 columns. NOTE: I have tried:
SET GLOBAL local_infile = 'ON';
but I still cannot get
LOAD DATA LOCAL INFILE 'File2.txt'
to work. For this reason, I am instead using:
LOAD DATA INFILE 'File2.txt'
Next, I add a 4th column at the end of the table:
ALTER TABLE testtable
ADD column4 VARCHAR(255)
AFTER column3;
At this point, the 4th column is created.
Now, I want try to load the file named 'File2.txt' into rows 1,2,3 of column # 4:
Put values into column # 4, from a *.txt file that has one column:
LOAD DATA INFILE 'File2.txt'
INTO TABLE testtable
FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'
(#col1,#col2,#col3,#col4) set column4=#col4;
View table:
SELECT * FROM testtable
Problem:
When I do this, the contents of the file 'File2.txt' are added to rows 4,5,6 of column # 4. Rows 1,2,3 of column 4 are NULL. Rows 4,5,6 of columns 1,2,3 are NULL. This is not what I wanted. I do not want the null entries. My final table should be 3 rows X 4 columns.
Image showing problem:
File Contents:
File.txt (the columns are separated by tabs):
1 First line of text 7.24
2 Second line goes here 5.7
3 Sample text on line 3 11.6
File2.txt:
before today
one tow
ssf three
Question:
Is there a way to load the file (like a horizontal concatenation) into rows 1,2,3 of column #4?

Loading a CSV file in a table using sqlloader

I have CSV file having two columns id_a and id_b, but I need to insert 4 more columns; ie. emp_sal_a, emp_sal_b, emp_dept_a, emp_dept_b using sqlldr. So my current control file looks like:
load data
infile '/home/.../employee.txt'
into table employee
fields terminated by ","
( id_a, id_b,
emp_sal_a ":id_a+1000", emp_sal_b "id_b+1000", emp_dept_a "10", emp_dept_b "20")
But I am getting error:
invalid binding variables
From MySQL Load Data Ref
note: search for the "(" character and it's the 35th instance of it on the page
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;
#var1 is the name of a variable you want to run an operation on, and what you're doing is calling SET on column2 to be equal to #var1/100.

Importing CSV files into MySql table with one column as name of CSV files

I have 35 CSV files which I want to import to MYSQL table(say 'test'). I want to create one column in 'test' table( say 'file_name'). This column will contain name of the CSV from which data has been imported. The file names are unique IDs, that is why I want to get file name as input in the table.
Suppose I have CSV files like X1.csv, X2.CSV, X3.csv .... X35.csv. I want a column in 'test' table as 'file_name' such that 'test' table looks something like:
col1 -> a, b, c, d
col2 -> x, y, w, z
...
...
... ....
file_name -> X1, X1, X2, X3
Note: I tried to search this question on forum but I could not find any suitable solution. Also I am new to MYSQL, please help even it is a trivial thing.
I'm not sure this is exactly what you are looking for, but at first sight, you should investigate the LOAD DATA INFILE statement:
LOAD DATA INFILE 'X1.csv' INTO TABLE tbl_name -- Load the content of the CSV file
FIELDS TERMINATED BY ',' ENCLOSED BY '"' -- assuming fields separate by ",", enclosed by "'"
LINES TERMINATED BY '\r\n' -- assuming end-of-line being '\r\n'
IGNORE 1 LINES -- assuming first line is a header and should be ignored
SET file_name = 'X1'; -- force the column `file_name` to be the name of the file
Please note that with such statement, each field will go in its own column of the table. And each line of the CSV data file will be loaded a one row in the table. This will imply that there will be several rows in the result table with the same file name. In fact one row per data line.

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