MatthiasRoelandts
MatthiasRoelandts

Reputation: 157

Adding variables to char *

I would like to change the values in my char * to the int and double that are declared so I can change them without touching char *sql. (on ubuntu using C)

How can i do this?

code:

int sensor_id = 10;

double temp = 22.22;

char *sql = "INSERT INTO test_table(sensor_id, sensor_value) VALUES(10, 22.22)";

Upvotes: 0

Views: 40

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50776

Try this (dirty, no error checking, buffer overflow may occur):

const char *sqlformat = 
            "INSERT INTO test_table(sensor_id, sensor_value) VALUES(%d, %f)";

char sql[200];
sprintf(sql, sensor_id, temp);

Now you have what you want in the sql buffer.

This is very basic C stuff, I suggest you to learn C.

Upvotes: 2

Related Questions