0

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?

  • 2
    What have you tried? Generally you need to run a SQL statement, and I don't see one in the code you've provided. To run something against MySQL in a batch file you need to use this thing: https://dev.mysql.com/doc/refman/5.5/en/mysql.html. I've never used it before, I just googled. It shows you how to connect to a database, but you'll need to provide more information about "save to a remote MySQL database". Save what? – Nick.Mc Mar 18 '18 at 01:17
  • Save VAR_1 and VAR_2 I also ask how to connect - obviously I know nothing about how to connect with a bt file. Thank you. –  Mar 18 '18 at 01:27
  • Can you first read the linked page and make an effort, then post your attempt. Then describe how you want to save those values. To a table? which table? do you want to insert new ones every time or just have one record with the latest values. Do you have an existing database with a table in it? – Nick.Mc Mar 18 '18 at 01:31
  • Nick.McDermaid - so "shell" commands can be made in a bat file? Why so elusive - I don't get it... if you know how... Why not just post an answer. I know php and mysql and use it daily - I know nothing of bat and shell and getting values from windows to online - that is why I am here. I am looking for examples to learn from - so I can make my own connection and save variables to a basic table in MySQL. I posted the question so I could learn and get on the right track about how to do this. –  Mar 18 '18 at 18:59
  • ..........and this is why I am so elusive and do not post an answer... because the OP never bothers coming back – Nick.Mc Mar 26 '18 at 12:58
  • As I said.. .sorry - was pulled away by work. Swamped. I will try to get back to this soon. I do not mean to be rude :) –  Mar 27 '18 at 21:43

1 Answers1

0

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

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
  • 1
    @Stnfordly it appears that you are now the elusive one – Nick.Mc Mar 26 '18 at 13:02
  • Yes, sorry. I have been pulled away. I will get back to this within the next few days.Thanks very much. –  Mar 27 '18 at 21:41
  • 1
    Cool no worries. It happens to everyone (including me when I ask a question!) – Nick.Mc Mar 28 '18 at 07:50
  • Dude whats up?? – Nick.Mc Apr 25 '18 at 23:18
  • Sorry man - still getting to this - had some huge meltdowns. Been working 3 weeks solid without a day off... I have a few days off coming up next week - I'll try it then. Thanks again man :) (Y) –  Apr 28 '18 at 21:11