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?

0


source to share


4 answers


You can use sprintf, but in C ++ you can do:

std::ostringstream buf;
buf << "SELECT * FROM Customers Where Customers.Employee = '" << id  << "' ";
string str = buf.str();

      



(unverified)

+3


source


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?

+2


source


use

std::ostringstream buf; buf << "SELECT * FROM Customers Where Customers.Employee = " << id ; string str = buf.str();

      

This should work, please try "12" --- no quote should be placed before or after 12

+1


source


you can use boost :: format with boost :: str

string = boost::str(boost::format("This is a string with some %s and %d numbers") %"strings" %42);

      

it should be better because you will have the whole replacement variable in one place at the end.

0


source







All Articles