Created
January 27, 2017 12:24
-
-
Save sanfx/640192daae31751ee28403330e36fcec to your computer and use it in GitHub Desktop.
using string stream in cpp and returning from subroutines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sstream> | |
#include <stdio.h> | |
#include <string.h> | |
using namespace std; | |
char* getSql() { | |
float outdoorTempInC = 15; | |
int outoorHumidity = 23; | |
float indorTempinC = 20; | |
int humidity = 22; | |
int val = 400; // soil sensor reading | |
int avgVal = 38; // avaerage of 10 values of soils sensor | |
char statusMsg[50] = {"Hello World"} ; | |
std::stringstream sqlQuery; | |
sqlQuery << "INSERT INTO arduinoSensorData.sensorLog (out_temperature, " | |
<< "out_humidity, drwngRoom_temperature, drwngRoom_humidity, " | |
<< "pot1_soilMoisture, pot1_avg_SoilMoisture, wateringPot1) VALUES ('" | |
<< outdoorTempInC << "', '" << outoorHumidity << "', '" << indorTempinC | |
<< "', '" << humidity << "', '" << val << "', '" << avgVal << "', '" | |
<< statusMsg << "');"; | |
char *mychar = new char[sqlQuery.str().length() + 1]; | |
strcpy(mychar, sqlQuery.str().c_str()); | |
return mychar; | |
} | |
string getSqlStream() { | |
float outdoorTempInC = 15; | |
int outoorHumidity = 23; | |
float indorTempinC = 20; | |
int humidity = 22; | |
int val = 400; // soil sensor reading | |
int avgVal = 38; // avaerage of 10 values of soils sensor | |
char statusMsg[50] = {"Hello World"} ; | |
std::stringstream sqlQuery; | |
sqlQuery << "INSERT INTO arduinoSensorData.sensorLog (out_temperature, " | |
<< "out_humidity, drwngRoom_temperature, drwngRoom_humidity, " | |
<< "pot1_soilMoisture, pot1_avg_SoilMoisture, wateringPot1) VALUES ('" | |
<< outdoorTempInC << "', '" << outoorHumidity << "', '" << indorTempinC | |
<< "', '" << humidity << "', '" << val << "', '" << avgVal << "', '" | |
<< statusMsg << "');"; | |
return sqlQuery.str(); | |
} | |
int main() | |
{ | |
string sql = getSqlStream(); | |
cout << getSql() << "\n"; | |
cout << sql.c_str() << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment