Custom #! to run file of SQL commands - mysql

I am looking for a way to have a simple way to execute SQL commands. One attempt is to make a custom #! script but I am not sure how to do this either.
If I do something like:
#!/bin/cat -n
select
col1
from
table;
I get output like
1 #!/bin/cat -n
2 select
3 col1
4 from
5 table;
Which makes me think I could be close.
But when I create a script like runsql.sh
#!/bin/bash
cat -n
./some_sql.sh: line 2: syntax error near unexpected token `newline'
./some_sql.sh: line 2: `select'
This is my attempt at being able to execute sql files. Is there someway people are doing this that I am not doing?
Thank you

try it:
mysql -e "YOUR_SQL_COMMAND"
for large command try it (i dont test but need work):
sqlCommand=$(cat <<EOF
This is large
Sql command
This is line three.
EOF
)
mysql < $sqlCommand

Related

Mysql query to update multiple rows using a input file from linux

I'm trying to update multiple rows in a DB using a small script.
I need to update the rows based on some specific user_ids which I have in a list on Linux machine.
#! /bin/bash
mysql -u user-ppassword db -e "update device set in_use=0 where user_id in ()";
As you see above, the user_ids are in a file, let's say /opt/test/user_ids_txt.
How can I import them into this command?
This really depends on the format of user_ids_txt. If we assume it just happens to be in the correct syntax for your SQL in statement, the following will work:
#! /bin/bash
mysql -u user-ppassword db -e "update device set in_use=0 where user_id in ($(< /opt/test/user_ids_txt))";
The bash interpreter will substitute in the contents of the file. This can be dangerous for SQL queries, so I would echo out the command on the terminal to make sure it is correct before implementing it. You should be able to preview your SQL query by simply running the following on the command line:
echo "update device set in_use=0 where user_id in ($(< /opt/test/user_ids_txt))"
If your file is not in the SQL in syntax you will need to edit it (or a copy of it) before running your query. I would recommend something like sed for this.
Example
Let's say your file /opt/test/user_ids_txt is just a list of user_ids in the format:
aaa
bbb
ccc
You can use sed to edit this into the correct SQL syntax:
sed 's/^/\'/g; s/$/\'/g; 2,$s/^/,/g' /opt/test/user_ids_txt
The output of this command will be:
'aaa'
,'bbb'
,'ccc'
If you look at this sed command, you will see 3 separate commands separated by semicolons. The individual commands translate to:
1: Add ' to the beginning of every line
2: Add ' to the end of every line
3: Add , to the beginning of every line but the first
Note: If your ID's are strictly numeric, you only need the third command.
This would make your SQL query translate to:
update device set in_use=0 where user_id in ('aaa'
,'bbb'
,'ccc')
Rather than make a temporary file to store this, I would use a bash variable, and simply plug that into the query like this:
#! /bin/bash
in_statement="$(sed 's/^/\'/g; s/$/\'/g; 2,$s/^/,/g' /opt/test/user_ids_txt)"
mysql -u user-ppassword db -e "update device set in_use=0 where user_id in (${in_statement})";

Subquerying MYSQL to select All Databases from server

I've checked this topic before asking, and I found it was helpful just in a certain point, and now it's not enough.
I have a lot of databases in a MariaDB server, and one of the registered views is broken, giving me "Invalid references" error.
What I need is to find out what View is broken, considering that this error is preventing me to run my Backup routine, and MariaDB log shows only "Database (Got error: 1049) (Locktable)"
Now that I have a list of my views, is there any way to "select *" all of them, in different databases?
I've tried something like this:
select * from TABLE_SCHEMA where (select TABLE_SCHEMA,TABLE_NAME from information_schema.tables where TABLE_TYPE like 'VIEW');
But I know it will not work since I'm not providing a specific database.
I think a Inner-Join would solve this case, but I can't figure out how to accomplish it...
By the way, if there's a better approach to find what View is defective, I would appreciate it.
Thank you in advance!
Since the only answer was removed by it's contributor, I've developed a ugly but effective workaround:
I've created a file with both Database and View names, using query provided below:
mysql -p*** -e "SELECT TABLE_SCHEMA, TABLE_NAME FROM information_schema.`TABLES` WHERE TABLE_TYPE LIKE 'VIEW'" > views.txt
This saves to file "views.txt" the flow with the information I need to run a basic Select in a View, so I did the following:
while IFS= read -r line; do mysql -p*** -e "select * from $(echo $line | awk '{print $1}').$(echo $line | awk '{print $2}') LIMIT 1" 2>> errors.txt; echo $line >> errors.txt; done < views.txt
I know it's ugly, but it will run a "select *" in every database.table or database.view listed in "views.txt", returning only first line of this query.
It provided me a effective test in my server, which showed me what view was broken:
ERROR 1146 (42S02) at line 1: Table '.' doesn't exist
If you're facing similar problems, I hope you may find a better way to solve this.

How can I get the number of affected records, and just that, using mysql cmdline?

I'm running the mysql cmdline binary to execute queries. (Specifically, I'm executing
LOAD DATA LOCAL INFILE). I would like to have it print, after every statement, the number of records effected - but no other lines. I don't mind the line containing other info, e.g.
Query OK, 20 rows affected, 4 warnings
would be great. I can, in fact, get that - but only if I use the -v -v pair of switches, and then I get more info which I don't need, like an echo of the parsed command.
Can I do anything other than grep the output for the pattern of the line above?
It's maybe not the best solution, but you can try the SQL ROW_COUNT() function
E.g :
mysql ... -e "YOUR_QUERY;SELECT ROW_COUNT()" -B | tail -n 1

how to use mysql fields in shell

I have table ,and i need access them from shell script and do the following.
> USERNAME="usr" PASSWORD="pwd"
> DBNAME="mydb"
>
> mysql -u$USERNAME -p$PASSWORD $DBNAME<<EOF
> selectfield1,field2,field3,field4 from table;
> EOF
gives me the records.But i need to process each fields with normal linux commands-
for example;
> mysql -u$USERNAME -p$PASSWORD $DBNAME<<EOF
> select field1,field2,field3,field4 from table;
> EOF
> scp field1#field2: field3#field4:/tmp && rm -rf field2
something like that - basic stuff is to use the database records for invoking linux commands on local machine.
One approach would be to use this construct (try it out, the syntax is a bit weird):
cat << EOF | while read a b c ; do echo "a:$a b:$b c:$c" ; done
one two three
EOF
[Edit: obviously replace cat and one two three with your actual sql stuff]
You will need to make sure that mysql does not print anything other than the result (no logo/copyright notice/version info header etc...) - I believe there are command line switches for that.
You'll also run into problems if there are spaces in the data returned, or if it's separated by something else than just whitespace. For those, you can usually get away with setting $IFS to something that never happens in your columns (maybe |), and making your query output that character between two fields.
Thanks for the answers and suggestions - But I found following method much more easier.
Is it safe to use this method?
variable=`mysql -u$USERNAME -p$PASSWORD $DBNAME <<EOF
select name,pass,email,flag from UserRemap where flag="Y" ;
EOF`
echo $variable
name=`echo $variable | cut -d' ' -f5`
pass=`echo $variable | cut -d' ' -f6`
mail=`echo $variable | cut -d' ' -f7`

Using MySQL in Powershell, how do I pipe the results of my script into a csv file?

In PowerShell, how do I execute my mysql script so that the results are piped into a csv file? The results of this script is just a small set of columns that I would like copied into a csv file.
I can have it go directly to the shell by doing:
mysql> source myscript.sql
And I have tried various little things like:
mysql> source myscript.sql > mysql.out
mysql> source myscript.sql > mysql.csv
in infinite variation, and I just get errors. My db connections is alright because I can do basic table queries from the command line etc... I haven't been able to find a solution on the web so far either...
Any help would be really appreciated!
You seem to not be running powershell, but the mysql command line tool (perhaps you started it in a powershell console though.)
Note also that the mysql command line tool cannot export directly to csv.
However, to redirect the output to a file just run
mysql mydb < myscript.sql >mysql.out
or e.g.
echo select * from mytable | mysql mydb >mysql.out
(and whatever arguments to mysql you need, like username, hostname)
Are you looking for SELECT INTO OUTFILE ? dev.mysql.com/doc/refman/5.1/en/select.html – Pekka 19 hours ago
Yep. Select into outfile worked! But to make sure you get column names you also need to do something like:
select *
from
(
select
a,
b,
c
)
Union ALL
(Select *
from actual)