c++sqlselectmysql++

how to pass a value of table to a variable of c++ using select in mysql++


How to select a value from a table and store in a variable in my sql++

e.g select name from employee;

write this query in c++ and then store the name in variable e_name


I googled and I know the mysql++ tutorials tell this code but I was connecting differently... can you see what is the problem with this.

mysql_init(&mysql); connection = mysql_real_connect(&mysql,..........)

dheck for connection else do this

std::ostringstream query3;
query3<<"select pipe_id from pipe where version_id='"<<id<<"'";
std::storeQueryResult ares=query3.store();
for(size_t i=0;i<ares.num_rows();i++)
    cout<<ares[i]["version_id"]<<ares[i]["pipe_id"]<<std::endl;
mysql_query(&mysql,query3.str().c_str());

the erroer is that store is not a member of ostringstream. thats i understood but in above so how should i proceeed any hints


Solution

  • See the mysql++ tutorial:

    http://tangentsoft.net/mysql++/doc/html/userman/tutorial.html#simple

    The relevant code:

    mysqlpp::Query query = conn.query("select item from stock");
    if (mysqlpp::StoreQueryResult res = query.store()) {
        cout << "We have:" << endl;
        for (size_t i = 0; i < res.num_rows(); ++i) {
            cout << '\t' << res[i][0] << endl;
        }
    }
    

    In this example, the results of the query are stored in the res variable.