Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Passing C++ variables to MySQL query

Posted on 2004-11-03
4
Medium Priority
?
633 Views
Last Modified: 2012-06-21
Hi, does anybody know how to pass C++ variables to MySQL query.

For example:

int y=6;
mysql_query(connection,"update table set column = y");

The update will fail since "y" is not recognized as a C++ variable
how should I modify the above statement so that "y" could be interpreted as "6"?

Thanks!
0
Comment
Question by:qwe123qwe123
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:pb_india
ID: 12484430
Try this:
int y=6;

char temp[32]="";
atoi(y,temp,10);

string str= "update table set column = ";
str=str+temp;
mysql_query(connection, (char*)str.c_str());

0
 

Author Comment

by:qwe123qwe123
ID: 12484621
The following are the errors during compilation:

error C2660: 'atoi' : function does not take 3 parameters
error C2065: 'string' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'str'
error C2065: 'str' : undeclared identifier
error C2440: '=' : cannot convert from 'char [38]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2440: '=' : cannot convert from 'char *' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2228: left of '.c_str' must have class/struct/union type

7 error(s), 0 warning(s)

Any Hints?
0
 
LVL 2

Accepted Solution

by:
pb_india earned 260 total points
ID: 12484680
sorry it it not atoi..

It is: itoa

Also you did not include <string.h>, that's why you got compile errors.

The below would work -


int y=6;

char temp[1024]="";
itoa(y,(char*)temp,10);

char str[1024]="";
strcpy(str,"update table set column = ");

strcat(str,temp);
mysql_query(connection, (char*)str);
0
 

Author Comment

by:qwe123qwe123
ID: 12484930
It works fine
thx
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question