I'm having a really hard time believing this question has never been asked before, it MUST be! I'm working on a batch file that needs to run some sql commands. All tutorials explaining this DO NOT WORK (referring to this link:Pass parameters to sql script that someone will undoubtedly mention)! I've tried other posts on this site verbatim and still nothing is working.
The way I see it, there are two ways I can approach this:
1. Either figure out how to call my basic MYSQL script and specify a parameter or..
2. Find an equivalent "USE ;" command that works in batch
My Batch file so far:
:START
#ECHO off
:Set_User
set usrCode = 0
mysql -u root SET #usrCode = '0'; \. caller.sql
Simply put, I want to pass 'usrCode' to my MYSQL script 'caller.sql' which looks like this:
USE `my_db`;
CALL collect_mismatch(#usrCode);
I know that procedures are a whole other topic to get into, but assume that the procedure is working just fine. I just can't get my parameter from Batch to MYSQL.
Ideally I would like to have the 'USE' & 'CALL' commands in my Batch file, but I can't find anything that let's me select a database in Batch before CALLing my procedure. That's when I tried the above link which boasts a simple command line entry and you're off to the races, but it isn't the case.
Any help would be greatly appreciated.
This will work;
echo SET #usrCode = '0'; > params.sql
type params.sql caller.sql | mysql -u root dbname
Related
When I input a code or anything into MySQL and hit "enter" it moves down and "->" appears. It is as if the code is not going through or the code is not being read.
I have attempted to download "add-ons" but I am really not sure what I am doing. This is for school and I am having trouble getting in touch with the professor.
I am new to this and can't figure out what I am doing wrong. Please help!
Please see image of what it looks like to me.
Please add semicolon ; after the mysql code.
Problem 1: Be aware of the prompt. Either of these
MariaDB >
mysql >
means that you are inside the MySQL commandline tool. You can enter only SQL statements. Most SQL queries need to be terminated by a ; or \G (but not both). To exit that tool:
exit
Or, if you get stuck in certain ways
CTRL-C
exit
Each of these implies a shell script:
$
#
mymachine$
/usr/home/rj $
C:\Users\rj:
and many others
Problem 2: mysqldump is a command, not SQL. So it needs to be executed in a shell script.
Problem 3: There is yet another problem. When it suggested typing 'help;', it did not mean for you to include the quotes. Instead, type just help;.
I'm new to Phing.
I'd like to query a value in a MySQL database table, and have the value set as a property so that I can echo it out nicely to the screen.
I can see that there is a PDOSQLExecTask which would allow me to run some SQL, but I can't see how to set the returned value into a property?
The query I want to run is:
SELECT MAX(change_number)
FROM changelog;
I'd like it set into a property:
Can anyone shed any light please?
Thanks,
Chris
I have access to MySQL at command line, I went with the following solution. I'm sure it's not the best, if someone else can improve it please do!
<!-- What's the latest delta that's been applied to this deployment? -->
<exec
command="${progs.mysql} -h${db.host} -u${db.user} -p${db.pass} -e 'USE ${db.main_db}; SELECT MAX(`change_number`) FROM `changelog`;'"
dir="."
checkreturn="false"
passthru="false"
outputProperty="latest_version_output"
/>
<php expression="preg_replace('/[^0-9]|\r|\n/si', '', '${latest_version_output}');" returnProperty="latest_version_applied" />
<echo msg="Latest delta applied was: ${latest_version_applied}" />
The PDOSQLExecTask comes with two default formatters, which will send their output to a file. To change this, you'd probably have to implement your own formatter. On the other hand, the task appears to read its SQL commands from a separate file with SQL commands, not the build file.
So on the whole, It seems to me like you might be better of writing your own task, probably using some code from the implementation of PDOSQLExecTask but with your own command input and result output. Unless calling the mysql command line binary is an alternative for you, in which case you could wrap up that call to redirect its output to a property using the outputProperty attribute to the ExecTask.
I have a php/mysql app that users can actually test/submit data to, so I want to delete/drop all the tables in my database and run my sql script to recreate and populate all the tables every hour.
So, in my cpanel, I have a cron job [see below]
/home/lwarinz/call_nw_test_script.sh
#!/bin/bash
mysql -hlocalhost -ulwarinz_hawaii -pdbAdmin67 -elwarinz_northwind \
</home/lwarinz/nw_test_Script.sql;
Note: I have have the query to drop and recreate the tables, but I am using this short query for testing:
USE lfwebz_northwind;
UPDATE employees SET FirstName = "Elizabeth" WHERE EmployeeID = 3;
but no matter what I change/adjust, I get the error below:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use near
'lwarinz_northwind' at line 1
I have changed so many things and still nothing works.
I know the db, user and password are correct. I wrote a small db connect code to test, so I am sure. Since I am new to this, write cron jobs, I don't know what to check. Could anyone give me any ideas on what I need test, change, move, etc.?
Any help/suggestions would be greatly appreciated.
/home/lwarinz/call_nw_test_script.sh
#!/bin/bash
mysql -hlocalhost -ulwarinz_hawaii -pdbAdmin67 lwarinz_northwind </home/lwarinz/nw_test_Script.sql;
Because you're specifying the database on command line, dont include the USE as part of your script..
#!/bin/bash
mysql -hlocalhost -ulwarinz_hawaii -pdbAdmin67 lwarinz_northwind \
</home/lwarinz/nw_test_Script.sql;
Script:
UPDATE employees SET FirstName = "Elizabeth" WHERE EmployeeID = 3;
Just wanted to leave this note. I kept plugging away at this and finally got to the point that I knew I had the script correct. Contacted my site provider, for the 4th time, and just made the last guy look at the script and explain why exactly it didn't work. Long story short, there was something "off" in my space that needed to be adjusted [still not sure I believethat], so it works now.
Thanks for everyone's input, I do appreciate it.
I want to create a deployment script, somehow emulate Oracle deployment scripts, where with ¶m you can use previously declared parameters.
I need to call this script for different users on different databases automatically.
For example my script should be:
USE ¶m;
DROP TABLE IF EXISTS `TEST` ;
CREATE TABLE IF NOT EXISTS `TEST` (X INT(16))
etc....
Of course ¶m is what I would have used in Oracle environment.
Thanks
Updates:
Forgot to mention that I am using a windows environment for now. I have created a batch script to call the mysql script. The easiest way I thought would be to pass to mysql 2 command: 1) use the schema I have as parameter and then call the script which will create the table regardless of the schema. Unfortunately mysql seems to understand that I want to connect to the schema X, but doesn't want to call the script.
REM param is the schema and mainsql is the script
SET param="%1"
SET mainsql="script.sql"
echo %param%
echo %mainsql%
mysql -u <user> --password=<psw> %param% "source %mainsql%;"
As far as I know you can't directly pass variables in to a MySQL script. The best you can do is set user variables in a wrapper shell script. Something like:
passed_var1=$1
passed_var2=$2
mainsql=script.sql
mysql $(usual_parameters) -e "set #user_var1=$passed_var1; set #user_var2=$passed_var2; source $mainsql"
Adjust for actual use, of course.
I've been trying to get a shell(bash) script to insert a row into a REMOTE database, but I've been having some trouble :(
The script is meant to upload a file to a server, get a URL, HASH, and a file size, connect to a remote mysql database, and insert the data into an existing table. I've gotten it working until the remote MYSQL database bit.
It looks like this:
#!/bin/bash
zxw=randomtext
description=randomtext2
for file in "$#"
do
echo -n *****
ident= *****
data= ****
size=` ****
hash=`****
mysql --host=randomhost --user=randomuser --password=randompass randomdb
insert into table (field1,field2,field3) values('http://www.example.com/$hash','$file','$size');
echo "done"
done
I'm a total noob at programming so yeah :P
Anyway, I added the \ to escape the brackets as I was getting errors. As it is right now, the script is works fine until connects to the mysql database. It just connects to the mysql database and doesn't do the insert command (and I don't even know if the insert command would work in bash).
PS: I've tried both the mysql commands from the command line one by one, and they worked, though I defined the hash/file/size and didn't have the escaping "".
Anyway, what do you guys think? Is what I'm trying to do even possible? If so how?
Any help would be appreciated :)
The insert statement has to be sent to mysql, not another line in the shell script, so you need to make it a "here document".
mysql --host=randomhost --user=randomuser --password=randompass randomdb << EOF
insert into table (field1,field2,field3) values('http://www.site.com/$hash','$file','$size');
EOF
The << EOF means take everything before the next line that contains nothing but EOF (no whitespace at the beginning) as standard input to the program.
This might not be exactly what you are looking for but it is an option.
If you want to bypass the annoyance of actually including your query in the sh script, you can save the query as .sql file (useful sometimes when the query is REALLY big and complicated). This can be done with simple file IO in whatever language you are using.
Then you can simply include in your sh scrip something like:
mysql -u youruser -p yourpass -h remoteHost < query.sql &
This is called batch mode execution. Optionally, you can include the ampersand at the end to ensure that that line of the sh script does not block.
Also if you are concerned about the same data getting entered multiple times and your rdbms getting inconsistent, you should explore MySql transactions (commit, rollback, etc).
Don't use raw SQL from bash; bash has no sane facility for sanitizing the data beforehand. Generate a CSV file and upload that instead.