Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL import from stdin

Tags:

bash

mysql

awk

I am generating a csv in stdout using awk. Is there a way to directly import that contents in mysql without putting it to file?

like image 388
Vivek Goel Avatar asked Oct 19 '12 06:10

Vivek Goel


People also ask

How do I import a database into MySQL?

Importing a database from a file To import a file, open Workbench and click on + next to the MySQL connections option. Fill in the fields with the connection information. Once connected to the database go to Data Import/Restore. Choose the option Import from Self-Contained File and select the file.

How do I import a csv file into MySQL?

In the Format list, select CSV. Changing format-specific options. If the csv file is delimited by a character other than a comma or if there are other specifications to the csv files, we can change it in this portion. Click Go to start importing the csv file and the data will be successfully imported into MySQL.


3 Answers

As the answer from @xdazz says, just use LOAD DATA LOCAL INFILE. I assume it was downvoted out of ignorance or lazyness. A quick perusal of the MySQL manuals would have shown that to be a perfectly viable answer.

In 2016 and for MariaDB, which will be most relevant to most users, you do this:

bash awk '{ /* My script that spits out a CSV */ }' | mysql --local-infile=1 -u user -ppassword mydatabase -e "LOAD DATA LOCAL INFILE '/dev/stdin' INTO TABLE mytable FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"';"

Obviously, do bother to read the manual and change the options to LOAD DATA INFILE as required to suit your specific case.

like image 165
user6710220 Avatar answered Oct 16 '22 19:10

user6710220


MySQL supports getting data in via extended inserts that look like this:

insert into table (col1, col2, col3) values
(a,b,c),
(d,e,f),
(g,h,i);

So you can modify your CSV to include a left paren and a right paren and a comma, and prepend it with insert into table... and append it with a semicolon, then you can pipe that directly to a MySQL commandline.

You can also use named pipes, a Unix construct, to pipe a TSV (tab-separated, not comma-separated) to a load data infile like this:

mkfifo /tmp/mysqltsv
cat file.csv | sed -e 's/,/\t/g' > /tmp/mysqltsv
mysql -e "load data infile '/tmp/mysqltsv' into table tblname"

That is pseudocode. You need to run the cat in one process and the mysql command in another. Easiest is to use two different terminals. More advanced is to background the cat|sed.

like image 32
philo vivero Avatar answered Oct 16 '22 17:10

philo vivero


It does not seem that you can import CSV from stdin directly. You have to save it to a file so that mysql uses its name as the name of the table (without the extension), you can use mysqlimport as in:

mysqlimport -uUSER -pPASS DB FILE

like image 1
hammady Avatar answered Oct 16 '22 19:10

hammady