tags:

views:

282

answers:

5

Hello,

after a lot of research i think its time to ask some questions. So this is how it goes. 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. I have a problem thought.

lets 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 wich is also integer. Any ideas? I would really appreciate some help here.

+2  A: 

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

See this question for how to do the conversion: http://stackoverflow.com/questions/228005/alternative-to-itoa-for-converting-integer-to-string-c

frankodwyer
+3  A: 

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
Stirngstream is the preferred way to go. Most queries are written with sqlstream.
fasih.ahmed
A: 

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
+1  A: 

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

wont do the job or any other conversion. How can i pass id as an integer in my query.?

You have ' quotes in your sql string around id, so your database is interpreting it as a string. Remove the ' quotes.
mch
mch you are the greatest :) i am confused right now because i thought i tried that.
+1  A: 

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