0

how can I write the different queries into the same file.

#!/bin/bash
while IFS= read -r line
do
mysql -udata -pdata database <<EOF
SELECT ID,name,grp,Status,ctime FROM database WHERE name LIKE '$line' INTO OUTFILE '/tmp/out.txt'
EOF
done < /tmp/input.txt



Error:
ERROR 1086 (HY000) at line 1: File '/tmp/out.txt' already exists

.

and with ">>" instead of "INTO OUTFILE" is error:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '>> '/tmp/out.txt'' at line 1

1 Answer 1

1

Put the redirection on the command line, not in the here document.

while IFS= read -r line
do
mysql -udata -pdata database <<EOF >> /tmp/out.txt
SELECT ID,name,grp,Status,ctime FROM database WHERE name LIKE '$line'
EOF
done < /tmp/input.txt

or

while IFS= read -r line
do
mysql -udata -pdata database <<EOF
SELECT ID,name,grp,Status,ctime FROM database WHERE name LIKE '$line'
EOF
done < /tmp/input.txt > /tmp/out.txt

Before running this code, though, be sure to read about SQL injection; it is generally a bad idea to construct dynamic SQL queries using string interpolation.

Sign up to request clarification or add additional context in comments.

1 Comment

I only want to use it once to read a lot of data. it is possible to add: FIELDS TERMINATED BY ';'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.