user2768380
user2768380

Reputation: 151

insert and edit in mysql rows from a file with bash

i am using bash to load a file in mysql, and i have:

mysql --local-infile=1 -u user data_base_1 < file.sql

and file.sql is :

..$ cat file.sql
 load data local infile '/folder/load.csv' into table table_1 fields terminated by '|'

The code works fine.

The problem is that if the PK of one row in the file exist, the row is not inserted, and i need if the row exist insert and replace the row in the table. How can i do it?

Who can help me?

Thanks

Upvotes: 0

Views: 193

Answers (2)

Bill Karwin
Bill Karwin

Reputation: 562230

You can specify REPLACE with LOAD DATA:

LOAD DATA LOCAL INFILE '/folder/load.csv' REPLACE INTO TABLE table_1 FIELDS TERMINATED BY '|'

Or else use the mysqlimport --replace option.

http://dev.mysql.com/doc/refman/5.6/en/mysqlimport.html#option_mysqlimport_replace

Upvotes: 1

FrankPl
FrankPl

Reputation: 13315

You could load into a temporary table and then execute two SQL statements:

UPDATE table
 WHERE ... (match found)
;
INSERT into table(...)
SELECT ...
  FROM temp_table
 WHERE NOT EXISTS(...)

Upvotes: 0

Related Questions