Mac unix script to insert row into Mysql database

Posted on 2011-09-19
Last Modified: 2012-06-21
I have wired the whole house with thermometers to keep track of the temperatures in the house.  I did it using Arduino Mega 2560.

Anyway, I have my Macmini polling the Arduino every 5 minutes to pick up the temperature in every room and logging it to a file.  But, I would like to log it to a database instead.

I have set up a MySql database and have successfully loaded the existing file to it..  so I know the database works,  but instead I would like to load one line of data to it every 5 minutes.  The code to load the data is.

======= CODE ======= from the command line ======

mysql -u sean -psomepassword password -e "INSERT INTO temperatures VALUES ('2011.09.17 10:35', 12.4,10.0,21.4,19.5,19.9,21.0,21.4,21.4,22.0,21.1,20.9,21.4,18.3);"


This works, because i've tried it....

My bash script nearly works, but dies when it comes to sending the above code,  Here is my bash script  (the echo and the final line are just checks I put in place to see if it was actually working properly.  I'm pretty sure the problem lies with my "escape" characters \  But I'm not sure. The buffer variable is just to prime the arduino, the first time I poll it, it doesn't actually work every time.

Could you take a look at it, or point me in the right direction?



THEDATETIME=`date +%Y.%m.%d" "%H:%M`
BUFFER=`/Users/seanroulet/Documents/Arduino/apps/arduino-serial -b 115200 -p /dev/cu.usbmodem641 -r`
NEWLINE=$DATETIME`/Users/seanroulet/Documents/Arduino/apps/arduino-serial -b 115200 -p /dev/cu.usbmodem641 -r`


$QUERY=`mysql -u sean -psomepassword cedro_azul_temperature -e \"INSERT INTO temperatures VALUES ("$NEWLINE");\""`

$QUERY >> ~/Documents/Arduino/temp.txt

Question by:sidartra

Accepted Solution

Ryan Weaver earned 500 total points
ID: 36566471
Two issues I can see with the script, both in this line:
$QUERY=`mysql -u sean -psomepassword cedro_azul_temperature -e \"INSERT INTO temperatures VALUES ("$NEWLINE");\""`

Open in new window

1, assigning to $QUERY should start the line without the '$'.
2, there is an extra " at the end of the line.

Author Comment

ID: 36571141
Thanks,  I've removed the $ from Query. and removed the extra "

Is the "\" backslash the correct "escape" character?  IE, I want to pass the " to the command, not close the variable.

I tried with your ammendments, but the error message remains.

I've changed the script slightly to have it echo the command I'm sending in $Query, and this is what I see

mysql -u sean -psomepassword cedro_azul_temperature -e "INSERT INTO temperatures VALUES ('2011.09.20 16:41', 22.9,22.1,21.5,21.5,21.2,21.9,21.8,21.7,22.7,22.5,21.4,21.7,19.4);"

then I copied the "echo" and pasted it in the command line, and it loaded the data!


Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you other experts are anything like me you are always looking into and testing out new features. While I was doing some research one day I ran across an app that I installed on my Mac and used as a security system. Mac OS X: SecureHome uses your …
The /etc/authorization file in Mac OS X 10.x can be used to control access to the various panes of the System Preferences amongst other things. It’s used by some of us Mac Sys Admin’s to give Standard Users access to System Prefs panes that only adm…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question