C ++ sql pass integer to sql string
I have created a database in MS Access. There I have a table called Customers that also has a cell called Employee type: integer. I also created a C ++ program that monitors all data.
Let's say I have a line like this:
string sqlString = "SELECT * FROM Customers Where Customers.Employee = '" + id + "' ";
The id passes through my function correctly and is an integer, so I get a compile-time error: "Invalid pointer addition".
If I declare id as a string, of course there is no error, but there are no results in my form. If I declare in Employee database cell as text and create my query like this:
string sqlString = "SELECT * FROM Customers WHERE Customers.Employee = 128";
I am getting results, but I need Employee as an integer to call its foreign key from another table.
So, what should I do with my query so that the results pass an integer as a parameter via a variable id to be ok with the Employee cell from the database, which is also an integer? Any ideas? I would really appreciate help here.
As I said, if I convert id to string, there are no results in my form since Employee in the database is an integer. So:
std::ostringstream buf;
buf << "SELECT * FROM Customers Where Customers.Employee = '" << id << "' ";
string str = buf.str();
will not do work or other transformation.
How can I pass id as an integer in my request?
You need to convert id to string, then your first approach should work.
See this question, How to do the conversion: An itoa () alternative for converting an integer to a C ++ string?
source to share