0

I have built a database in MS Access. There I have a table called Customers which also has a cell called Employee type: integer. I also built a program in C++ which controls all data.

Let's say I have a string like this:

string sqlString = "SELECT * FROM Customers Where Customers.Employee = '" + id + "' ";

Id passes through my function correctly and is an integer, so I get an error in compilation saying: "Invalid pointer addition".

If I declare id as a string of course there's no error but there are no results in my form also. If I declare in database cell Employee as text and build my query like this:

string sqlString = "SELECT * FROM Customers WHERE Customers.Employee = 128";

I get results, but I need that Employee as an integer cause its a foreign key from another table.

So, what should I do with my query to have results passing integer as parameter through variable id, to be ok with the cell Employee from database which is also integer? Any ideas? I would really appreciate some help here.


As I said, if I convert id to string, there are no results in my form since Employee in database is an integer. So this:

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

won't do the job or any other conversion.

How can I pass id as an integer in my query?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880

4 Answers4

3

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

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

(untested)

Bill Zeller
  • 1,336
  • 1
  • 9
  • 13
2

You need to convert id to a string, then your first approach should work.

See this question for how to do the conversion: Alternative to itoa() for converting integer to string C++?

Community
  • 1
  • 1
frankodwyer
  • 13,948
  • 9
  • 50
  • 70
1

use

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

This should work please try '12' --- quote should not be placed before and after 12

yesraaj
  • 46,370
  • 69
  • 194
  • 251
0

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);

this should be better approach since you will have all the replacement variable in one place at the end.

yesraaj
  • 46,370
  • 69
  • 194
  • 251