I have trouble getting a JWT for a server to server application.
With the following command
openssl pkcs12 -in mypkcs12-private.p12 -out privateKey.pem.
Following the doc /docs/OAuth2ServiceAccount,
I encoded my header
{"alg":"RS256","typ":"JWT"}
and my claim
{"iss":"******#developer.gserviceaccount.com","scope":"https://www.googleapis.com/auth/drive.file,"aud":"https://accounts.google.com/o/oauth2/token","exp":1401710837,"iat":1401707237}
in base64. so I created the base64 encoded signature using (b64header.b64claim), my private key and HMAC function. When I request the token using curl, I get an invalid_grant error. I tried many things but I don't know where the issue is.
here is the code
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
MemoryStruct *mem = (MemoryStruct *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
char *base64encode(const char *normal, size_t length)
{
BIO *bmem, *b64;
BUF_MEM *bptr;
char *buff = NULL;
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, normal, (int) length);
if (BIO_flush(b64) != 1) {
return NULL;
}
BIO_get_mem_ptr(b64, &bptr);
buff = (char *) malloc(bptr->length);
memcpy(buff, bptr->data, bptr->length-1);
buff[bptr->length - 1] = 0;
BIO_free_all(b64);
b64 = NULL;
return buff;
}
char * createClaimSet(char * address)
{
char * output = NULL;
char claim_format[] = "{\"iss\":\"%s\",\"scope\":\"https://www.googleapis.com/auth/drive.file\",\"aud\":\"https://accounts.google.com/o/oauth2/token\",\"exp\":%ld,\"iat\":%ld}";
size_t len_address = 0;
size_t size = 0;
time_t iat;
time_t exp;
time(&iat);
exp = iat + 3600;
if (address) len_address = strlen(address);
size = strlen(claim_format);
output = (char*) calloc(len_address + size - 8 + 20 + 1, sizeof(char));
sprintf(output,claim_format, address, exp, iat);
printf(output);
return output;
}
char *b64HmacSha256fct(char *line, char * keyp12, size_t size)
{
char *Hmac64 = "";
const EVP_MD *evp_md = EVP_sha256();
unsigned char md[EVP_MAX_MD_SIZE];
unsigned int md_len;
// HMAC
HMAC(evp_md, keyp12, (int)size, (const unsigned char*) line, strlen(line), md, &md_len);
Hmac64=base64encode(md, md_len);
return Hmac64;
}
int AuthServiceAccount(CURL *curl)
{
char * b64_claim_set = NULL;
char * claim_set = NULL;
char * header64 = NULL;
char * JWT = NULL;
char * toSign = NULL;
char * sig64 = NULL;
char * str = NULL;
char * data = NULL;
char * privateKey = NULL;
char grant[] = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer";
char uri[] = "https://accounts.google.com/o/oauth2/token";
char header[] = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}";
char emailAddress[] = "xxxxxx#developer.gserviceaccount.com";
char PKCS12_FILE_PATH[] = "C:\\temp\\mykey.key";
FILE * pInputP12Name = NULL;
int i = 0;
int j = 0;
MemoryStruct writeMemory;
size_t fileP12size = 0;
size_t lclaim64 = 0;
size_t lclaimSet = 0;
size_t lheader64 = 0;
size_t lstr = 0;
size_t resultat;
header64 = base64encode(header, strlen(header));
if (header64) lheader64 = strlen(header64);
claim_set = createClaimSet(emailAddress);
if (claim_set) lclaimSet = strlen(claim_set);
b64_claim_set = base64encode(claim_set, lclaimSet);
if (b64_claim_set) lclaim64 = strlen(b64_claim_set);
str = (char*) calloc(lclaim64+1, sizeof(char));
if (!str) return 1;
for (i=0; i<lclaim64; i++) {
if (isprint(b64_claim_set[i])) {
str[j] = b64_claim_set[i];
j++;
}
}
str[j] = '\0';
if (str) lstr = strlen(str);
toSign = (char*) calloc(lheader64 + 1 + lstr + 1, sizeof(char));
if (!toSign) return 1;
sprintf(toSign,"%s.%s",header64,str);
pInputP12Name = fopen (PKCS12_FILE_PATH,"rb");
if (pInputP12Name==NULL) {
perror ("Error opening file");
return (-1);
}
else
{
resultat = _fseeki64(pInputP12Name, 0, SEEK_END);
fileP12size= (size_t) ftell(pInputP12Name);
rewind(pInputP12Name);
privateKey = (char *) calloc(fileP12size+1, sizeof(char));
resultat = fread (privateKey,sizeof(char),fileP12size,pInputP12Name);
fclose (pInputP12Name);
}
sig64 = b64HmacSha256fct(toSign,privateKey,fileP12size);
if (!sig64) return 1;
JWT = (char *) calloc(strlen(toSign) + 1 + strlen(sig64) + 1, sizeof(char));
sprintf(JWT,"%s.%s", toSign, sig64);
if (curl) {
FILE* pfile = NULL;
size_t size = 0;
memset(&writeMemory,'\0',sizeof(MemoryStruct));
size = strlen(grant)+strlen("&assertion=")+strlen(JWT);
data = (char *) calloc(size+1, sizeof(char));
if (!data) return 1;
sprintf(data,"%s&assertion=%s", grant, JWT);
curl_easy_reset(curl);
curl_easy_setopt( curl, CURLOPT_URL, uri );
curl_easy_setopt( curl, CURLOPT_PORT , 443 );
curl_easy_setopt( curl, CURLOPT_POST, 1 );
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt( curl, CURLOPT_POSTFIELDSIZE, strlen(data));
curl_easy_setopt( curl, CURLOPT_HEADER, 1 );
curl_easy_setopt( curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt( curl, CURLOPT_WRITEDATA, &writeMemory);
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt( curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt( curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_perform(curl);
printf("%s",writeMemory.memory);
}
return 0;
}
int main(void)
{
CURL *curl;
curl_version_info_data *CurlVers;
CurlVers = curl_version_info( CURLVERSION_NOW );
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
AuthServiceAccount(curl);
/* always cleanup */
curl_easy_cleanup(curl);
curl_global_cleanup();
exit(0);
}
The documentation states that you must use base64url encoding, which is a variant of base64 encoded that uses URL-safe characters.
Related
I am writing a program where the user is giving random 10 bytes which I am storing in a buffer. Then search the hexadecimal string in a MySql database.
unsigned char buffer[10]; //Example:- {23, 181, 185, 192, 101, 156, 61, 247, 206, 204}
//convert each byte into 2 digit hexadecimal characters and store it in an array.
unsigned char hex_bytes[21];
for(int i = 0, l = 0; i < 10; i++) {
int decimal_num = buffer[i];
int quotient, remainder;
char hex_num[10];
quotient = decimal_num;
int j = 0;
while(quotient != 0) {
remainder = quotient % 16;
if(remainder < 10) {
hex_num[j++] = 48 + remainder;
}
else {
hex_num[j++] = 87 + remainder; //hex_num[j++] = 55 + remainder;
}
quotient = quotient/16;
}
for(int k = j-1; k >=0; k--) {
hex_bytes[l++] = hex_num[k];
}
}
printf("Ticket no. = %s\n", hex_bytes);
//------------------------------------------------------------------//
//Querying the Database
unsigned char query1[256];
uint32_t nos[10000];
MYSQL_RES *result;
//unsigned char* hex_str = "17b5b9c0659c3df7cecc"; <-----
//sprintf(query1, "SELECT sn FROM fixit_log WHERE rn = '%s'", hex_str); <-----
sprintf(query1, "SELECT sn FROM fixit_log WHERE rn = '%s'", hex_bytes);
mysql_query(con, query1);
if(mysql_errno(con) != 0) {
printf("stderr: %s\n", mysql_error(con));
mysql_close(con);
return;
}
result = mysql_store_result(con);
int k = 0;
for(int i =0; i < sr_nos_size; i++) {
MYSQL_ROW row = mysql_fetch_row(result);
printf("--sn: %2s\n", row[0]);
}
}
My Database looks like this:
sn rn
123456 17b5b9c0659c3df7cecc ///hexadecimal string of the given example as stored in the database
123457 17b5b9c0659c3df7cecc
123458 17b5b9c0659c3df7cecc
If I hardcode the hexadecimal string(comment out // part), I am getting the proper output. But if I run the program as above I am don't get any output.
I have put the value coming out of the the packet[i] from the for loops in variables d,e f,j. but when I am trying to put the values of these variables in a column of mysql I get an error
Unknown column 'd' in 'field list'
My code:
printf("\nResponse Code: ");
for(i = 54; i<=56; i++)
{
d = packet[i];
printf("%c", packet[i]);
}
printf("\nCommand: ");
for(i = 54; i<=57; i++)
{
e = packet[i];
printf("%c", packet[i]);
}
printf("\nResponse Parameter: ");
for(i = 58; i<=104; i++)
{
f = packet[i];
printf("%c", packet[i]);
}
printf("\nTime to live:");
j = packet[22];
printf("%c", packet[i]);
if (mysql_query(con, "CREATE TABLE SMTP(Response_Code CHAR(250) , Command CHAR(250), Response_Parameter CHAR(250), Time_to_live CHAR(250))")) {
finish_with_error(con);
}
printf("here");
if (mysql_query(con, "INSERT INTO SMTP VALUES(d,e,f,j)")) {
finish_with_error(con);
}
I want the values of d e f and j to be printed in these columns but I am getting error Unknown column 'd' in 'field list'
You can use snprintf or sprintf to form the query, before to that extract the complete data instead of single char from the packet.
char d[100], e[100], f[100];
printf("\nResponse Code: ");
for(i = 54; i<=56; i++)
{
d[i-54] = packet[i]; //Copy the individual char
printf("%c", packet[i]);
}
d[i-54] = '\0'; //null terminate the array
printf("\nCommand: ");
for(i = 54; i<=57; i++)
{
e[i-54] = packet[i];
printf("%c", packet[i]);
}
e[i-54] = '\0';
printf("\nResponse Parameter: ");
for(i = 58; i<=104; i++)
{
f[i-58] = packet[i];
printf("%c", packet[i]);
}
f[i-58] = '\0';
//do the same for even "Time to live"
Then form the prepared statement and execute it,
char query[300];
sprintf(query, "INSERT INTO SMTP VALUES (?,?,?)");
MYSQL_STMT *stmt;
MYSQL_BIND bind[3];
stmt = mysql_stmt_init(con);
if (!stmt)
{
return;//error
}
if (mysql_stmt_prepare(con, query, strlen(query)))
{
return;//error
}
int dLen = strlen(d);
bind[0].buffer_type= MYSQL_TYPE_STRING;
bind[0].buffer= (char *)d;
bind[0].buffer_length= STRING_SIZE;
bind[0].is_null= 0;
bind[0].length= &dLen;
int eLen = strlen(e);
bind[1].buffer_type= MYSQL_TYPE_STRING;
bind[1].buffer= (char *)e;
bind[1].buffer_length= STRING_SIZE;
bind[1].is_null= 0;
bind[1].length= &eLen ;
int fLen = strlen(f);
bind[2].buffer_type= MYSQL_TYPE_STRING;
bind[2].buffer= (char *)f;
bind[2].buffer_length= STRING_SIZE;
bind[2].is_null= 0;
bind[2].length= &fLen;
/* Execute the INSERT statement - 1*/
if (mysql_stmt_execute(stmt))
{
return; //error
}
mysql_stmt_close(stmt
I have a temperature sensor hooked up to a Raspberry Pi, I am able to read and printf the temperature. What I am trying to do next is to get the values from the sensor and have them get logged into a MySQL database.
Everything works, except, I can not figure out how to format the MySQL insert Query to use the f variable and the h variable that is generated by the read_dht_data() function and pass it to the MIA_temp_insert(float f, float h).
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <wiringPi.h>
#include <stdint.h>
#define MAX_TIMINGS 85
#define DHT_PIN 3 /* GPIO-22 */
int data[5] = { 0, 0, 0, 0, 0 };
float temp_f;
static char *host = "XXX.XXX.XXX.XXX";
static char *user = "XXX";
static char *password = "XXX";
static char *dbname = "XXX";
unsigned int port = 3306;
static char *unix_socket = NULL;
unsigned int flag = 0;
static MYSQL *conn; //Needed to be static so all of the functions can draw from it
void MIA_mysql_connection()
{
// Connecting to the Database
conn = mysql_init(NULL);
if (!(mysql_real_connect(conn, host, user, password, dbname, port, unix_socket, flag)))
{
fprintf(stderr, "\n Error: %s [%d] \n", mysql_error(conn),mysql_errno(conn));
exit (1);
}
//Connected
printf ("We Are Connected \n");
}
void MIA_mysql_close()
{
//Closing Connection
mysql_close(conn);
}
void MIA_temp_insert(float f, float h)
{
//Inserting into MySQL Table
if(mysql_query(conn, "INSERT INTO `temperature` (`id`, `Date`,`Time`, `Temperature`, `Humidity`) VALUES (NULL, CURRENT_DATE(), CURRENT_TIME(), '%f' , '%f')") !=0)
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit (-1);
} else {
printf("Rows were insert \n");
}
}
void read_dht_data()
{
uint8_t laststate = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
/* pull pin down for 18 milliseconds */
pinMode( DHT_PIN, OUTPUT );
digitalWrite( DHT_PIN, LOW );
delay( 18 );
/* prepare to read the pin */
pinMode( DHT_PIN, INPUT );
/* detect change and read data */
for ( i = 0; i < MAX_TIMINGS; i++ )
{
counter = 0;
while ( digitalRead( DHT_PIN ) == laststate )
{
counter++;
delayMicroseconds( 1 );
if ( counter == 255 )
{
break;
}
}
laststate = digitalRead( DHT_PIN );
if ( counter == 255 )
break;
/* ignore first 3 transitions */
if ( (i >= 4) && (i % 2 == 0) )
{
/* shove each bit into the storage bytes */
data[j / 8] <<= 1;
if ( counter > 16 )
data[j / 8] |= 1;
j++;
}
}
/*
* check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
* print it out if data is good
*/
if ( (j >= 40) &&
(data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
{
float h = (float)((data[0] << 8) + data[1]) / 10;
if ( h > 100 )
{
h = data[0]; // for DHT11
}
float c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
if ( c > 125 )
{
c = data[2]; // for DHT11
}
if ( data[2] & 0x80 )
{
c = -c;
}
float f = c * 1.8f + 32;
//printf( "Humidity = %.1f %% Temperature = %.1f *C (%.1f *F)\n", h, c, f );
printf ("Temp of the room is : %.1f \n",f);
//Insert Data into MIA Temperature Table
MIA_temp_insert(10.0,11.1);
}else {
//printf( "Data not good, skip\n" );
}
}
int main ()
{
printf( "Raspberry Pi DHT11/DHT22 temperature/humidity test\n" );
if ( wiringPiSetup() == -1 )
exit( 1 );
while ( 1 )
{
MIA_mysql_connection();
read_dht_data();
delay (1000);
MIA_mysql_close();
printf("End of program \n");
}
return(0);
}
You cannot use printf()-style format specifiers in mysql_query() directly as you have above. Instead, you can use sprintf() or snprintf() to write the format string -- with the formatted numbers instead of format specifiers -- into a buffer... then pass that as your query string.
So in MIA_temp_insert(), instead of the following:
if(mysql_query(conn, "INSERT INTO `temperature` (`id`, `Date`,`Time`, `Temperature`, `Humidity`) VALUES (NULL, CURRENT_DATE(), CURRENT_TIME(), '%f' , '%f')") !=0)
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit (-1);
/* ...etc... */
...you could try this:
/* Your original query string, with the %f specifiers */
const char *formatstring = "INSERT INTO `temperature` (`id`, `Date`,`Time`, `Temperature`, `Humidity`) VALUES (NULL, CURRENT_DATE(), CURRENT_TIME(), '%f' , '%f')";
/* snprintf() the query into a buffer, to fill in the numbers */
char buf[256]; // should be large enough for the query with the numbers filled in
if (snprintf(buf, sizeof(buf), formatstring, f, h) >= sizeof(buf))
{
exit(-1); // if snprintf() returned sizeof(buf) or more, buf was too short
}
/* call mysql_query() with the formatted query from the buffer */
if (mysql_query(conn, buf) != 0)
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit (-1);
/* ...etc... */
Does Microsoft have a good FFT that I can use ?
so I made my owe FFT and it work from some case but now all...
like if I get it
f(t) =10*sin(2*pi *3000*t) + 20*sin(1000* 2* PI* t)
it will work but if I add
+ 5*sin(2*pi*100*T) is start acting fun?
now in Matlab it works good but not in my close, also my fft only seem to return the right numbers in the Image not so much in the real...
here is my code:
enter code here
public struct polar1
{
public double real;
public double img;
};
private float Fs;
private int N;
private polar1 [] F;
private int R;
public DSPclass(float[] DSP1,int f1)
{
N = DSP1.Length;
R = DSP1.Length;
F = new polar1[N];
Fs = (float)f1;
}
public void FFT1(float[] DSP1)
{
polar1[] x = new polar1[DSP1.Length];
for (int v = 0; v < N; v++)
{
x[v].real = DSP1[v];
x[v].img = 0;
}
F = FFT(x);
int temp;
}
public polar1[] FFT(polar1[] x)
{
int N2 = x.Length;
polar1[] X = new polar1[N2];
if (N2 == 1)
{
return x;
}
polar1[] odd = new polar1[N2 / 2];
polar1[] even = new polar1[N2 / 2];
polar1[] Y_Odd = new polar1[N2 / 2];
polar1[] Y_Even = new polar1[N2 / 2];
for (int t = 0; t < N2 / 2; t++)
{
even[t].img = x[t * 2].img;
even[t].real = x[t * 2].real;
odd[t].img = x[(t * 2) + 1].img;
odd[t].real = x[(t * 2) + 1].real;
}
Y_Even = FFT(even);
Y_Odd = FFT(odd);
polar1 temp4;
for (int k = 0; k < (N2 / 2); k++)
{
temp4 = Complex1(k, N2);
X[k].real = Y_Even[k].real + (Y_Odd[k].real * temp4.real);
X[k + (N2 / 2)].real = Y_Even[k].real - (Y_Odd[k].real * temp4.real);
X[k].img = Y_Even[k].img + (Y_Odd[k].real * temp4.img);
X[k + (N2 / 2)].img = Y_Even[k].img - (Y_Odd[k].real * temp4.img);
}
return X;
}
public double magnitude( polar1 temp)
{
double tempD;
tempD = Math.Sqrt ( (temp.img * temp.img) + (temp.real * temp.real));
return tempD;
}
public polar1 Complex2(int K, int N, int F3)
{
polar1 temp;
double temp1;
temp1 = (2D * K *F3) / N;
if (temp1 % 2 == 0 || temp1 == 0)
{
temp.real = 1D;
temp.img = 0D;
}
else if ((temp1 - 1) % 2 == 0)
{
temp.real = -1D;
temp.img = 0D;
}
else if ((temp1 / .5D) - 1 % 2 == 0)
{
if ((temp1 - .5D) % 2 == 0)
{
temp.real = 0D;
temp.img = -1D;
}
else
{
temp.real = 0D;
temp.img = 1D;
}
}
else
{
temp.real = Math.Cos(temp1 * Math.PI);
temp.img = -1D * Math.Sin(temp1 * Math.PI);
}
return temp;
}
public polar1 Complex1(int K, int N3)
{
polar1 temp;
double temp1;
temp1 = (2D * Math.PI *K) / N3;
temp.real = Math.Cos(temp1);
temp.img = Math.Sin(temp1);
return temp;
}
public int Apm(double[] img, double[] real)
{
for (int i = 0; i < R; i++)
{
img[i] = F[i].img;
real[i] = F[i].real;
}
return R;
}
public int frequencies(float [] freq, float [] Ctemp)
{
bool flag = false;
bool flagD = false;
float tempOld = 0;
float tempNew =0;
int tempc = 0;
int counter = 0;
for (int i = 0; i < R; i++)
{
if (((i / N) * Fs) >= (Fs / 2))
{
return counter;
}
if ((int)F[i].img != 0 )
{
flag = true;
tempOld = (float)(Math.Abs(F[i].img));
}
else
{
if (flagD == true)
{
freq[counter] = ((float)tempc / (float)N) * Fs;
Ctemp[counter] = tempNew; //magnitude(F[tempc]);
counter++;
flagD = false;
}
flag = false;
tempOld = 0;
tempNew = 0;
}
if(flag == true)
{
if (tempOld > tempNew)
{
tempNew = tempOld;
tempc = i;
flagD = true;
}
}
}
return counter;
}
}
I created this table (results) in my sql database (test)
CREATE DATABASE `test`;
USE `test`;
CREATE TABLE `results` (
`number` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`id_machine` int(10) unsigned NOT NULL,
`value` float NOT NULL,
`datetime` datetime NOT NULL,
PRIMARY KEY (`number`),
UNIQUE KEY `indice_UNIQUE` (`number`)
) ENGINE=InnoDB AUTO_INCREMENT=100;
My external device gives me these results:
+DATA: 43 BYTES FROM 0000:0000 (045)
Machine_8: (T=22.22, HR=42.56, Dw=8.95, VCC=3.64V)
and with the usage of strtok I get some values of these results to save them in the database:
Results: 8, 22.22, 42.56, 8.95, 3.64
I would like to save my data in my table in this way:
101, 8, 22.22, 2013-06-05 14:03:00
102, 8, 42.56, 2013-06-05 14:03:00
103, 8, 8.95, 2013-06-05 14:03:00
104, 8, 3.64, 2013-06-05 14:03:00
This is my code in my function until now
int learn_port2(int fd)
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_RES *res1;
MYSQL_ROW row;
char *server = "127.0.0.1";
char *user = "root";
char *password = "***"; // got tot keep my data secret
char *database = "test";
conn = mysql_init(NULL);
if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return -1;
//finish_with_error(conn);
}
int n, i;
char buff[300];
memset(buff, 0, sizeof(buff));
for (int x = 0; x<1; x++)
//for (;;)
{
char id_machine[35] = "";
char temp[35] = "";
char hum[35] = "";
char dw[35] = "";
char vol[45] = "";
char* ptr;
int i,nodo,nodo1;
float temp, hum, dw, vcc;
n=read(fd,buff,sizeof(buff));
sleep(1);
printf("%s", buff);
printf("\n");
if (buff[37] == 'N' || buff[38] == 'N' || buff[39] == 'N' || buff[40] == 'N' )
{
ptr = strtok(buff, "Machine_,=T:HR:DW:Vcc()");
i = 0;
while (ptr != NULL)
{
if (i == 9)
strcat(id_machine, ptr); // copies Nodo
if (i == 10)
strcat(temp, ptr); // copies T
if (i == 11)
strcat(hum, ptr); // copies HR
if (i == 13)
strcat(dw, ptr); // copies DW
if (i == 15)
strcat(vol, ptr); // copies Vcc
ptr = strtok(NULL, "Machine_,=T:HR:DW:Vcc()");
i++;
}
printf("Results: %s, %s, %s, %s, %s\n", id_machine, temp, hum, dw, vol);
}
char query[]="INSERT INTO results(`id_machine`,`value`,`valor`) VALUES(id_machine,'14',value)";
if(mysql_query(conn, query))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return -1;
}
res = mysql_use_result(conn);
}
}
How can I modify the char query[] to have the results which I want? Or if there are some examples like this.
To make a parameterized query you could use something like the following...
The example uses vsnprintf() to take a printf like format string and a variable number of arguments after it to print the query into a temporary buffer and then pass the buffer to the MYSQL query. In this way you can get a paramaterized query...
vsnprintf() accepts a variable parameter list, va_list and also is buffer-overflow safe.
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#define BUFSTRLEN 255
/* Returns -1 if the internal buffer was not large enough to hold the formatted
* string, or a format error occurred. If neither condition occurred then the
* result of the MYSQL query is returned... */
int DoMysqlQuery(MYSQL *conn, char const *printfstring, ...)
{
int len;
va_list args;
char buffer[BUFSTRLEN + 1];
memset(buffer, '\0', sizeof(buffer));
va_start(args, printfstring);
len = vsnprintf(buffer, BUFSTRLEN, printfstring, args);
va_end(args);
/* Did the buffer print work ? */
if( len < 0 || len >= BUFSTRLEN + 1 )
return -1;
return mysql_query(conn, buffer);
}
int main(int argc, char const *argv[])
{
int result;
MYSQL mysqlConn = ...;
/* Example stuff to send to the function.... */
char *id_machine= "8";
char *value= "1234";
char *valor= "1";
/* Send the query */
result = DoMysqlQuery(
&mysqlConn,
"INSERT INTO results(`id_machine`,`value`,`valor`) VALUES(%s,'%s',%s);",
id_machine, value, valor);
if( result )
{
/* handle error etc... *.
}
}
In this example, the query string that will be sent to mysql_query() is:
INSERT INTO results(id_machine,value,valor) VALUES(8,'1234',1);
Hope this helps :)