Thursday, 26 September 2013

Grab specific data using C++ with MySQL

Grab specific data using C++ with MySQL

I am trying to grab specific information from a MySQL database in C++.
Lets assume I can connect and have my MySQL package working just fine. The
below picture is a database. I want to retrieve the name LISA and insert
it into a variable to be used later. Anyone have any idea how to do so?
The below code works too.
DATABASE:
ID NAME
1 Rico
2 John
6 Lisa
7 Max
This is my current output with the below code. I just want LISA and be
able to place in a variable.
Input id: 6
name:
6Lisa
CODE:
string id_query;
string outstr;
string str8 = "Select * From users WHERE id=";
cout << "Input id: ";
cin >> id_query;
outstr = str8.c_str() + id_query;
if (mysql_query(conn, outstr.c_str()))
{
cerr << mysql_error(conn) << endl;
cout << "Press any key to continue. . . ";
cin.get();
exit(1);
}
res = mysql_use_result(conn);
cout << "name: " << endl;
while ((row = mysql_fetch_row(res)) != NULL)
cout << "\t" << row [0] << row[1] << endl;
cin.get();

No comments:

Post a Comment