I tried to run a command prompt and log in into mysql.
Run(#ComSpec & ' /k' & 'cd'&' '&#ScriptDir&'/mysql/bin')
this works perfectly. But When I add the mysql command like below, it doesn't work. It displays a 'path not found' error message:
Run(#ComSpec & ' /k' & 'cd'&' '&#ScriptDir&'/mysql/bin/mysql u- root -p')
I would appreciate if anyone could show me the correct way to go.
You don't need to change your directory (cd) when you want to actually execute a program. The error message tells you, there is no directory #ScriptDir/mysql/bin/mysql - and there isn't. The directory #ScriptDir/mysql/bin/ most likely exists and there most likely is an executable mysql inside...
So just use two commands separated by ; instead:
Run(#ComSpec & ' /k' & 'cd ' & #ScriptDir & '/mysql/bin; mysql -u root -p')
You need to separate between the cd and the actual call of mysql. And you'd propably want to correct u- to -u.
Related
Hope you're having a great day thus far :)
I'm using this script to automate an sqlquery, so that another team can use these logs. However, they are not permitted to have the login credentials for this database, and my current script uses a cleartext password.
I suggested that we create a new database profile and limit the permissions of the profile, so that only the specific table can be accessed with read-only permissions. However, this isn't best practice, and my lead doesn't like the idea.
So, I'm trying to figure out a way to pass the login credentials through the script, without using cleartext.
Do you have any ideas?
This is the current format of the cmd I'm running in the batch file:
sqlcmd -S server.database.windows.net -U user#domain -P password -d DB_Name -i "c:\users\%USERNAME%\desktop\Blue Prism Audit Logs\eventdatetime24hr.sql" -o "c:\users\%USERNAME%\desktop\Blue Prism Audit Logs\Audit Logs\queryOut%DATE:~4,2%_%DATE:~7,2%_%DATE:~-4%_%time:~0,2%%time:~3,2%.csv"
Expand the SQL Server Agent node and right click the Jobs node in SQL Server Agent and select 'New Job'
In the 'New Job' window enter the name of the job and a description on the 'General' tab.
Select 'Steps' on the left hand side of the window and click 'New' at the bottom.
In the 'Steps' window enter a step name and select the database you want the query to run against.
Paste in the T-SQL command you want to run into the Command window and click 'OK'.
Click on the 'Schedule' menu on the left of the New Job window and enter the schedule information (e.g. daily and a time).
Click 'OK' - and that should be it.
(There are of course other options you can add - but I would say that is the bare minimum you need to get a job set up and scheduled)
example tsql code with output
DECLARE #cmd sysname, #var sysname;
SET #var = 'Hello world';
SET #cmd = 'echo ' + #var + ' > var_out.txt';
EXEC master..xp_cmdshell #cmd;
more info here: https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/xp-cmdshell-transact-sql?view=sql-server-ver15
this will create a script that runs whenever you want it to and you set it up on the DB and then you can decide where you put the output i.e. in a target location for the other team to pick it up.
I normally work with PHP/MySQL. A client wants to send variables from a .bat file - to a remote MySQL - where I will then manipulate them for display etc. I do not know how to connect and send these variables from a bat file in Windows.
I have small .bat file on windows, that simply writes a few variables to a text file.
#echo off
#echo Data: > test.txt
#echo VAR_1=777 >> test.txt
#echo VAR_2=245.67 >> test.txt
The result of the .bat file is a text file test.txt created with various details in it.
I would like the .bat file commands to also:
1) connect to a remote MySQL database
connect -> '8580922.hostedresource.com'
2) save to a basic table on a remote MySQL database:
INSERT INTO `My_Database`.`My_Table` (
`VAR_1` ,
`VAR_2` ,
)
VALUES (
'777',
'245.67'
);
Is this possible?
Is so - how?
I don't have MySQL Installed and I'm not familiar with it but here is a crack at something to try, based on info from the linked page.
REM This needs to be set to the right path
set bin=C:\Program Files\MySQL\MySQL Server 5.6\bin
REM set the host name and db
SET DBHOST=8580922.hostedresource.com
SET DBNAME=MyDatabase
REM set the variables and the SQL
SET VAR_1=777
SET VAR_2=245.67
SET SQL="INSERT INTO `My_Database`.`My_Table` (`VAR_1`,`VAR_2`) VALUES ( '%VAR_1%',
'%VAR_2%');"
"%bin%/mysql" -e %SQL% --user=NAME_OF_USER --password=PASSWORD -h %DBHOST% %DBNAME%
PAUSE
Please try that and post back the resulting error message. There are many reasons that it won't work, but you need to try it to find out.
I'm not sure where test.txt comes into this but it would be a good idea export the whole SQL statement to a text file then use the correct MySQL command line switch to just run the file instead of generating the SQL inside the batch file.
There's a bit more here.
connecting to MySQL from the command line
I am getting this error when I am trying to import my existing database to localhost. The database imports to web host servers but importing to the localhost.
The error is;
Static analysis:
2 errors were found during analysis.
Ending quote ' was expected. (near "" at position 28310)
4 values were expected, but found 3. (near "(" at position 28266)
PhpMyAdmin is kinda dumb since it cannot import what it itself exported. It escapes single quotes as '' instead of \' and then breaks its teeth on strings like this:
''I can''t do this anymore!''
You can either:
replace '' → \', or
import via mysql.exe:
mysql -uuser -ppass dbName < file.sql
open your .sql script file in any editor(like notepad++) and
You need to replace \'' with \' (for new version of phpmyadmin)
or
You need to replace \' with \'' (for old version of phpmyadmin)
when you will replace it from all content of sql file then
it will work for you.
ref:https://stackoverflow.com/a/41376791/2298211
This might happen because the database - size that you export is too big.
THE SOLUTION FOR ME WAS:
Choose from Export method:
Custom - display all possible options
Format: SQL
Output:
In Compression - choose the option zipped
export the database as zip , (ex: database_name.sql.zip)
import it on local, and from time to time if it throws an error for taking too long , you can resume the import, by press on resume and resubmit - and choose again the same database and will continue from where stopped before.
I attached a picture with these settings:
I have been using emacs for writing programs and recently I want to execute SQL queries through it , I run the following commands and get the following error:
M-x sql-Mysql
then I just press return and then
user:
password:
database:
server:
and I get the error
" unable to locate sql program mysql "
I have searched for two continuous days and got the suggestions to edit .emacs file but of no use. I am using windows 7.
My init file looks like this :
(setq sql-connection-alist
'((pool-a
(sql-product 'mysql)
(sql-server "1.2.3.4")
(sql-user "me")
(sql-password "mypassword")
(sql-database "thedb")
(sql-port 3306))
(pool-b
(sql-product 'mysql)
(sql-server "1.2.3.4")
(sql-user "me")
(sql-password "mypassword")
(sql-database "thedb")
(sql-port 3307))))
(defun sql-connect-preset (name)
"Connect to a predefined SQL connection listed in `sql-connection-alist'"
(eval `(let ,(cdr (assoc name sql-connection-alist))
(flet ((sql-get-login (&rest what)))
(sql-product-interactive sql-product)))))
(defun sql-pool-a ()
(interactive)
(sql-connect-preset 'pool-a))
well,i think,it does not need to put that code in my init file so i erased all that.in fact i need not put any thing in the init file for mysql to run.i just filled in the appropriate details in the following fields:
user:root
password:which i have created during mysql installation
database:i left this field blank
server:localhost
then no problem occurred and i could run the sql commands as usual as i would in a normal mysql command window.what i made out from it that server as 'localhost' and user as 'root' are the main parameters and should work in probably all the cases(if you are running mysql server on your system through emacs. password to be filled is the password entered during the time of installation.
I have a script that I need to execute using a batch file. Do I use SQLCMD in the batch file to run the script? Also, the script inserts data to a table in a database. How should I format the SQLCMD in the batch file so it knows what database it is suppose to work with?
First, save your query into an sql text file (text file with .sql extension). Make sure to add the USE statement at the beginning, which tells the server which database you want to work with. Using the example from MSDN:
USE AdventureWorks2008R2;
GO
SELECT p.FirstName + ' ' + p.LastName AS 'Employee Name',
a.AddressLine1, a.AddressLine2 , a.City, a.PostalCode
FROM Person.Person AS p
INNER JOIN HumanResources.Employee AS e
ON p.BusinessEntityID = e.BusinessEntityID
INNER JOIN Person.BusinessEntityAddress bea
ON bea.BusinessEntityID = e.BusinessEntityID
INNER JOIN Person.Address AS a
ON a.AddressID = bea.AddressID;
GO
Then in your batch file, you run SQLCMD and pass it the sql file (with path) as a parameter.
sqlcmd -S myServer\instanceName -i C:\myScript.sql
If you need to authenticate as well, you'll need to add in -U and -P parameters to your SQLCMD command.
Here's an MSDN article dealing with the sqlcmd utility with more details.
Use the -S switch to specify server and instance names, e.g. -S MyDbServer\Database1
SQLCMD documentation found here.
If you want to execute all .sql files (multiple sql scripts in a folder) for multiple database then create a batch file "RunScript-All.bat" with below content
echo "======Start - Running scripts for master database======="
Call RunScript-master.bat
echo "=======End - Running scripts for master database=========="
pause
echo "=====Start - Running scripts for model database========"
Call RunScript-model.bat
echo "=======End - Running scripts for master database=========="
pause
Definition for individual batch file for a specific database i.e. "RunScript-master.bat" can be written as per below
for %%G in (*.sql) do sqlcmd /S .\SQL2014 /U sa /P XXXXXXXXX /d master -i"%%G"
::pause
Create many files for different databases and call them from "RunScript-All.bat".
Now you will be all to run all sql scripts in many database by clicking on "RunScript-All.bat" batch file.