jakeheik90
jakeheik90

Reputation: 11

MySQL C API mysql_query

I have two problems with the MySQL C API. How do I get variable to be added to the table when I do the mysql_query() and how do I get the table show decimals?

Here is my code:

void insertDataToTable(MYSQL* con, float temp, int k)
{
    mysql_query(con, "INSERT INTO Weatherstation VALUES(@k,'Temperature:', @temp)");
}

Thank you!

Upvotes: 0

Views: 758

Answers (2)

mckenzm
mckenzm

Reputation: 1820

MySQL does not support host variables that well, the previous answer shows the correct approach in building the query into a string and then using mysql_query(someconnection, string).

What is absolutely shocking is that this is not documented as much as it should be. When I had to learn this I had 30 years of DB2 and C, and I had to get this from GitHub after 2 hours of fruitless searching for "MySQL Host Variables".

You should also be aware of mysql_real_query if your query is not a conventional string and contains embedded nulls, it is passed the length of the string, but can also move a strlen() call off the server.

Upvotes: 0

asio_guy
asio_guy

Reputation: 3767

Try this

void insertDataToTable(MYSQL* con, float temp, int k)
{
    char query[255] ;

    sprintf( query, "INSERT INTO Weatherstation VALUES(%ld,'Temperature:', %d)", temp, k );
    mysql_query(con, query );


}

Upvotes: 2

Related Questions