I am trying to insert some data using Load data infile into a mysql table that already has some data. The table contains id and name. My csv file contains three fields: id, name and code. The table schema also has these three fields, but currently, the table has NULL for the code field. I am trying to insert the code from csv to an existing row in the table if it matches the name, else I am trying to insert a complete new row.
The code I have tried is as follows:
LOAD DATA INFILE 'table1.csv'
INTO TABLE table1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(#code, #name, #other_columns)
IF EXISTS (SELECT * FROM table1 where name=#name);
BEGIN
set code=#Code;
END
ELSE
BEGIN
set code=#Code, name=#name;
END
By doing so, I am getting a mysql syntax error, but am unable to figure it out. Can anyone point me in the right direction, or suggest to me another approach? I have thousands of new rows to insert and thousands of existing rows to modify based on the certain field, (name in this case).
MySQL does not allow the LOAD DATA INFILE statement inside a stored program, which is where the IF statement appears. Break up your task into two parts. First, LOAD DATA INFILE into a temporary table. Then create a stored program that replaces the loaded data into your table1 from the temporary table.
Related
I have many files, for example: 20170319, 20170320 ...
For each file I have 2 columns, one for username and the other was data.
I've created a table
create table A(user varchar(35), date date, data varchar(35), primary key(user, date));
Then, I want to load those files into database, and use filename as specific date in date field.
Can I still use sth like:
Load data infile '20170320' into table A
The answer is that you cannot do this in MySQL alone, you need to use an external program or script that builds the load data infile statements with the appropriate SET clause derived from the name of the file:
The SET clause can be used to supply values not derived from the input
file. The following statement sets column3 to the current date and
time:
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, column2)
SET column3 = CURRENT_TIMESTAMP;
The reason for this is that:
load data infile cannot use the file name as an input variable
neither MySQL prepared statements using the prepare statement, nor stored procedures are allowed to use the load data infile statement.
I want to use LOAD DATA INFILE command of MYSQL to insert about 900,000 row to my table called INFO of a database called A.
the INFO tables is defined like below:
create table INFO (id varchar(10) PRIMARY KEY, receive_time datetime,
check_time datetime, ver varchar(10) NOT NULL)
before inserting in this table, just for testing, I create another database B, and create a table with the same definition of INFO called TMP_INFO. I run this query on the temporary database B and table TMP_INFO and it works:
LOAD DATA INFILE '/home/develop/list.txt' IGNORE INTO TABLE TMP_INFO
LINES TERMINATED BY '\n' IGNORE 1 LINES (id, #receive_time, #check_time, #ver)
set receive_time=NOW(), check_time=NOW(), ver='8754';
the list.txt file is similar to this :
8754
0087653565
0000986759
0000654327
0000453219
but the problem is that when I run the same query on the database A and INFO table, with the same list.txt file, it doesn't work. I get no error, but N warnings (N = number of lines in line.txt - 1 )!!! all the warnings are the same showed bellow:
warning code 1265:Data truncated for column 'id' at row ...
I am going to use this query within a C program.
I am confused, and don't have any idea, Any Idea please?
I am trying to load data from a CSV into a database in MySQL workbench. The table I am loading into has an auto increment ID column. I am trying to get the query to recognize that I want it to keep the first column as Null, and I put NULL as the value in the CSV, but I cannot get the SET ... NULL command to recognize the name of the ID column. Here is the SQL I am using:
load data infile 'filenam.csv'
INTO TABLE table_name
fields Terminated By ','
LINES TERMINATED BY ',,'
SET column_name = null
I suspect I am making a simple syntax error that is causing the problem. But I cannot figure out what it is.
If you put NULL as the value in the CSV file then you shouldn't need the "SET column_name = null" in the statement. AFAIK, the SET value should be used to supply values not derived from the input file or to perform calculations to the value before insertion. This statement should work fine since you said you specified NULL in the CSV. However, make sure you specified NULL "properly" according to the documentation. I always use \N in my import files.
LOAD DATA INFILE 'filename.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ','
LINES TERMINATED BY ',,'
Secondly, you can discard the NULL specified in the CSV file by assigning it to a user variable and then specifying the column value with SET. However, you need to specify a column list
LOAD DATA INFILE 'filename.csv'
INTO TABLE table_name (#dummy, column_2, column_3)
FIELDS TERMINATED BY ','
LINES TERMINATED BY ',,'
SET column_name = NULL
I have one other thought based on the MySQL docs and dependent upon how your server is configured. Comment if this does not work and I will provide more options.
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
i got to execute loads of the following queries
UPDATE translations SET translation = (SELECT description FROM content WHERE id = 10) WHERE id = 1;
Now, i use the load data infile to do inserts and replacements but what i want in esense is to update just 1 field for each row in that table with out messing with the keys. What could be the syntax for this, note that the queries affect existing rows only.
Thanx
Use CREATE TEMPORARY TABLE to create a temporary table.
Then use LOAD DATA INFILE to populate that temporary table.
Then execute your UPDATE translations SET translation = ... to set the 1 field from a SELECT of the temporary table, JOINed with the real table. example syntax below:
UPDATE realTable, tmpTable
SET realTable.price = tmpTable.price
WHERE realTable.key = tmpTable.key
I need to update existing rows in table with load data infile based on some condition, is this possible?
load data infile 'E:/xxx.csv'
into table tld_tod
#aaa, #xxx_date, #ccc
fields terminated by ','
LINES TERMINATED BY '\r\n'
set xxx = str_to_date(#xxx_date, '%d-%b-%y')
where xxx is not null and aaa=#aaa
You ca also create a staging table, insert the data from the CSV file into the staging table and then finally insert the data into your target table with the required operations and filtering.
CREATE TEMPORARY TABLE staging LIKE tld_tod;
LOAD DATA INFILE 'E:/xxx.csv'
INTO TABLE staging
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n';
INSERT INTO tld_tod
SELECT STR_TO_DATE(col_date, '%d-%b-%y') AS date
WHERE col_date IS NOT NULL;
In MySQL it's possible to create triggers before update. So in this case I suggest to use:
delimiter //
CREATE TRIGGER upd_check BEFORE UPDATE ON table
FOR EACH ROW
BEGIN
IF NEW.xxx IS NOT NULL THEN
SET NEW.xxx = 0;
END IF;
END;//
delimiter ;
After creating trigger, you can run load data infile without WHERE.
I'm not sure what's your specific required condition, but do it inside BEGIN and END.