# HTTPS POST to mySQL on localhost

**URL:** <https://community.particle.io/t/https-post-to-mysql-on-localhost/25545>\
**Category:** Getting Started\
**Created:** [September 2, 2016, 7:21am UTC](https://community.particle.io/t/https-post-to-mysql-on-localhost/25545 "2016-09-02T07:21:31Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [March 1, 2019, 8:33am UTC](https://community.particle.io/t/https-post-to-mysql-on-localhost/25545/21 "2019-03-01T08:33:07Z")

</div>

For one, you are doing exactly the same thing in all three `if()` blocks, so there is no need for a distinction between the cases.  
But if I understand correctly the main point is that you are receiving _three_ events independent/asynchronous of eachother where each of them _only_ provides **_one_** value. Hence you won’t be able to use a single `INSERT` statement which would expect to find **_all_** data points in the incoming JSON.

Before we can advise you better you need to tell us how you intend to synchronise your data as you probably want the three columns somehow related to eachother - or do you just want to re-insert any previous value of the _other two_ data points when any _one_ of the three gets reported?

BTW, you have `console.log("storeData", data)` in your store function.  
Have you had a look at that log and tried to figure _why_ your approach didn’t work?

---

<div class="post-metadata">

**Author:** ![alyssawrong](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alyssawrong](https://community.particle.io/u/alyssawrong)\
**Post date:** [March 8, 2019, 1:08am UTC](https://community.particle.io/t/https-post-to-mysql-on-localhost/25545/22 "2019-03-08T01:08:17Z")

</div>

Thanks for the reply, this is my first encounter with JavaScript so sorry if my question was difficult to understand. I do want all three sensors related to each other by the timestamp although I know they are sent less than a second from each other. I have one table with three columns (bpm, emg, gsr) and want them to be inserted together in the same row. I understand now that the cloudEventHandler gets each event separately and returns that data to the next function so I need to find a way to store all three data values at a time, and then send those three to mySQL before the next three are received.

---

<div class="post-metadata">

**Author:** ![alyssawrong](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alyssawrong](https://community.particle.io/u/alyssawrong)\
**Post date:** [March 22, 2019, 1:45am UTC](https://community.particle.io/t/https-post-to-mysql-on-localhost/25545/23 "2019-03-22T01:45:41Z")

</div>

Hello, I’ve actually solved my own question here and forgot to reply with my answer in case it will help anyone in the future.  
In my case, I am reading three different sensors, collecting the values from all three sensors on the cloud and inserting them into a mysql table containing all three in different columns. Here is my mysqlsample.js file I have edited to do so.

```auto
var config = require('./config.js');
var Particle = require('particle-api-js');
var particle = new Particle();

var mysql = require('mysql');
console.log("starting database connection");
var con = mysql.createConnection(config.mysql);
console.log("starting event stream listener");
var values = [[0,0,0]];

con.connect(function(err) {
	if (err) throw (err);
	console.log("Connected!")
});

particle.login({username: 'myemail@gmail.com', password: ' ********'}).then(
	function(data) {
		token = data.body.access_token;
	},
	function(err) {
		console.log('Could not login.', err);
});

particle.getEventStream({deviceId: 'e00fce68cb ****************', auth: config.authToken}).then(
	function(stream) {
		stream.on('event', cloudEventHandler);
	
	},
	function(err) {
		console.log("error starting event listener", err);
		process.exit(1)
	});
	
cloudEventHandler = function(data) {
	//console.log(bpmData, emgData, gsrData);
	
	if (data.name == 'bpm'){
		var	bpm = JSON.parse(data.data);
		values.splice(0,1,bpm);
		
	}	
	if (data.name == 'emg'){
		var emg = JSON.parse(data.data);
		values.splice(1,1,emg);
		
	}
	if (data.name == 'gsr'){	
		var gsr = JSON.parse(data.data);
		values.splice(2,1,gsr);
		storeData(values);
		
	}
	
	
}

function storeData(values) {
	var sql = "INSERT INTO sensors (bpm, emg, gsr) VALUES ?";
	console.log(values);
	con.query(sql, [[values]], function(err,result) {
		if (err) throw err;
		console.log("id=" + result.insertId);
		
 	});
}

```

This inserts all three individual sensor values into my table corresponding to the same time stamp. I have seen where one of my sensors publishes before the other two can publish again, and my code still knows to insert the correct ones into the corresponding column.  
Hope this can help someone in the future!

---

<div class="post-metadata">

**Author:** ![friedl\_1977](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/friedl_1977/32/22930_2.png) [@friedl\_1977](https://community.particle.io/u/friedl_1977)\
**Post date:** [May 22, 2019, 5:17pm UTC](https://community.particle.io/t/https-post-to-mysql-on-localhost/25545/24 "2019-05-22T17:17:06Z")

</div>

Hi Rickkasy -

With the help of ScrruffR I have managed to get the data I require to be sent successfully to Particle cloud, but the challenge for me is that from here I was hoping to send the data to a mySQL DB hosted on a shared server we have with Hetzner. We are building a UI that will in turn retrieve the data form the mySQL DB. I was hoping you can assist?

I have done quite a bit of research into existing cloud services but most of them seems overkill for our purposes as we only really require the data to be published into the SQL DB.

I am extremely new to this, so please accept my apologies is the question is more trivial than I think 🙂

Regards,  
Friedl

---

<div class="post-metadata">

**Author:** ![seaurchin](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@seaurchin](https://community.particle.io/u/seaurchin)\
**Post date:** [November 6, 2019, 10:14pm UTC](https://community.particle.io/t/https-post-to-mysql-on-localhost/25545/25 "2019-11-06T22:14:18Z")

</div>

This does work and i am also using but it is very slow. Establishing initial connection takes quite a bit of time sometimes even 1 minute or more.

---

<div class="post-metadata">

**Author:** ![bibanez](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@bibanez](https://community.particle.io/u/bibanez)\
**Post date:** [April 5, 2020, 3:17pm UTC](https://community.particle.io/t/https-post-to-mysql-on-localhost/25545/26 "2020-04-05T15:17:40Z")

</div>

Hi!

Thank you very much for your help. I used your example to fetch two sensors in a local mysql database. I can see in the console the two values being fetched correctly, but when they are inserted in the mysql database both columns appear as [Object object].

This is my example code, I know this has to do with the type of data but I wasn’t able to figure out the problem.

Any help would be appreciated! 🙂

```auto
// You will need to customize the settings in the config.js file!
var config = require('./config.js');

// Particle cloud API 
var Particle = require('particle-api-js');
var particle = new Particle();

// https://github.com/mysqljs/mysql
var mysql = require('mysql');

// Make database connection
console.log("starting database connection");

var connection = mysql.createConnection(config.mysql);

connection.connect(function(err) {
    if (err) throw (err);
    console.log("Connected!")
});
	
// Make cloud connection
console.log("starting event stream listener");
var values = [[0,0]];

particle.getEventStream({ deviceId: 'mine', auth: config.authToken }).then(
	function(stream) {
		stream.on('event', cloudEventHandler);
	},
	function(err) {
		console.log("error starting event listener", err);			
		process.exit(1);		
	});

cloudEventHandler = function(data) {

	if (data.name == 'Temp. BME280 corr: ') {
        var temp = JSON.parse(data.data);
        values.splice(0,1,temp);
	}
    if (data.name == 'Hum. BME280: ') {
        var hum = JSON.parse(data.data);
        values.splice(1,1,hum);
        storeData(values);
    }
}

function storeData(values) {
    var sql = "INSERT INTO HabPrincipal (Temperature, Humidity) VALUES ?";
    console.log(values);
    connection.query(sql, [[values]], function(err,result) {
		
        if (err) throw err;
		console.log("id=" + result.insertId);
	});
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [April 5, 2020, 4:19pm UTC](https://community.particle.io/t/https-post-to-mysql-on-localhost/25545/27 "2020-04-05T16:19:49Z")

</div>

Adding some `console.log()` statements at any place where your potential issue might occure should be one of the first steps to figure out what is going wrong.

That’s easiest done by _you_ since _you_ already have all the stuff setup as needed 😉

[Previous page](https://community.particle.io/t/https-post-to-mysql-on-localhost/25545.md?page=1)
