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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

We could spend the next millennium discussing the differences of the Mac and Windows platforms. The next century will continue to have fanatics on both side of the equation and neither side will win the war. However, that’s not why we are here. W…
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
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 …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

718 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