On MS SQL, I can do bulk insert using the sql command below:
BULK INSERT myDatabase.MyTable FROM 'C:\MyTextFile.txt' WITH FIELDTERMINATOR = ','
Now I want to do the same on MySQL but I can't seem to figure out how this works and what query to use.
In MySQL, the equivalent would be
LOAD DATA INFILE
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
LOAD DATA INFILE 'C:\MyTextFile'
INTO TABLE myDatabase.MyTable
FIELDS TERMINATED BY ','
Related
I need to create an import using infile to create a stored procedure.
How can I add username and password? I can't find documentation.
LOAD DATA INFILE 'data.csv' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/newdata.csv"
into table prodlookupfile
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES;
This executes but the new data is NOT uploaded into the table in MySQL, what am I missing?
I have tried the suggestion in the question below but I still have syntax errors.
How to LOAD DATA INFILE in mysql with first col being Auto Increment?
create table db.test
(ai_id int(11) auto_increment primary key,
field varchar(5))
LOAD DATA LOCAL INFILE 'C:\\Users\\nick\\Desktop\\test\\book1.csv'
INTO TABLE db.test
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(field)
SET ai_id = NULL
IGNORE 1 lines;
I am having trouble reconciling this seemingly very simple syntax error, any assistance greatly appreciated!
EDIT:
error code: 1064: You have error in SQL syntax; check syntax around 'ignore 1 lines' line 8.
datasource is a csv with one column "field" with five rows "one"-"five"(all five rows are characters not int)
This syntax is correct, I tested its working(in MySQL 5.6). please verify your input file.
The following works. It appears the order of commands is what threw it off.
LOAD DATA LOCAL INFILE 'C:\\Users\\nshatz\\Desktop\\test\\book1.csv'
INTO TABLE db.test
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 lines
(field);
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.
Say I have a view in my database, and I want to send a file to someone to create that view's output as a table in their database.
mysqldump of course only exports the 'create view...' statement (well, okay, it includes the create table, but no data).
What I have done is simply duplicate the view as a real table and dump that. But for a big table it's slow and wasteful:
create table tmptable select * from myview
Short of creating a script that mimics the behaviour of mysqldump and does this, is there a better way?
One option would be to do a query into a CSV file and import that. To select into a CSV file:
From http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/
SELECT order_id,product_name,qty
FROM orders
INTO OUTFILE '/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
OK, so based on your CSV failure comment, start with Paul's answer. Make the following change to it:
- FIELDS TERMINATED BY ','
+ FIELDS TERMINATED BY ',' ESCAPED BY '\'
When you're done with that, on the import side you'll do a "load data infile" and use the same terminated / enclosed / escaped statements.
Same problem here my problem is that I want to export view definition (84 fields and millions of records) as a "create table" statement, because view can variate along time and I want an automatic process. So that's what I did:
Create table from view but with no records
mysql -uxxxx -pxxxxxx my_db -e "create table if not exists my_view_def as select * from my_view limit 0;"
Export new table definition. I'm adding a sed command to change table name my_view_def to match original view name ("my_view")
mysqldump -uxxxx -pxxxxxx my_db my_view_def | sed s/my_view_def/my_view/g > /tmp/my_view.sql
drop temporary table
mysql -uxxxx -pxxxxxx my_db -e "drop table my_view_def;"
Export data as a CSV file
SELECT * from my_view into outfile "/tmp/my_view.csv" fields terminated BY ";" ENCLOSED BY '"' LINES TERMINATED BY '\n';
Then you'll have two files, one with the definition and another with the data in CSV format.