Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
mysql: execute queries from bash
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
pactoo
Guru
Guru


Joined: 18 Jul 2004
Posts: 553

PostPosted: Thu Aug 25, 2011 8:06 am    Post subject: mysql: execute queries from bash Reply with quote

Hello,

I am trying to do a simple query of a mysql database from bash. But I do not want to read that query from a script.

Something like:
mysql -u USER -pPASS -D database "select * from table;"

Instead of:
mysql -u USER -pPASS -D database < file.sql

Currently I am having trouble realising this
Back to top
View user's profile Send private message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Thu Aug 25, 2011 10:07 am    Post subject: Reply with quote

This is the way I do queries from bash:

Code:
echo "select * from table;" | mysql -u USER -pPASS -D database


If you don't want the column names displayed as the first row add -s to the mysql options. You can also change the output format: -H for HTML, -t for table, -E for vertical or -X for XML.
_________________
Search | Read | Answer | Report | Strip
Back to top
View user's profile Send private message
BrummBrumm
n00b
n00b


Joined: 04 Jan 2008
Posts: 58

PostPosted: Thu Aug 25, 2011 10:21 am    Post subject: Reply with quote

hi.

try this:

mysql --user="someuser" --password="pw" -D "dbname" -e 'your-sql-command;'


you can also do something like

mysql --user="user" --password="pw" << EOF
command-1;
command-2;
EOF
Back to top
View user's profile Send private message
pactoo
Guru
Guru


Joined: 18 Jul 2004
Posts: 553

PostPosted: Fri Aug 26, 2011 6:48 am    Post subject: Reply with quote

Thanks very much. For some reason, I have overseen (or misinterpreted) the "-e" option. Multiple times actually. However, the other hints may be helpful, too
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum