0

as the title says i want to do the following:

I created (in sql) a table "radiation" which is :

enter image description here

Now, i want from c++ to be able to ask the user "Give an element " and if the user gives for example "I" then retrieve the "halftime" field for example.

The c++ code is :

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <sys/time.h>
#include <mysql/mysql.h>


using namespace std;

int main()
{ 


MYSQL_RES *result;
MYSQL_ROW row;
MYSQL *connection, mysql;

const char* server="127.0.0.1";
const char* user ="****";
const char* password="****";
const char* database="***";

int state;

mysql_init(&mysql);

connection = mysql_real_connect(&mysql,server,user,password,database,0,0,0);

if (connection == NULL)
    {

cout<<mysql_error(&mysql);

return 1;
    }

state = mysql_query(connection, "SELECT * FROM radiation");

if (state !=0)
    {
    cout <<mysql_error(connection);
   return 1;
    }

result = mysql_store_result(connection);


row=mysql_fetch_row(result);
string name;

cout <<"\nGive the name : "<<endl;
cin >>name;

cout << "\nThe halftime for "<<name << " is "<<**row[2]**->here i want to extract the analogous filed <<" hours";
//}



mysql_free_result(result);

mysql_close(connection);


return 0;
}

I know only the basics from sql , i don't know how difficult is to do the above.

3
  • 1
    What's the question? How difficult is it? You've provided some code, so does that code work? What problems specifically with the code are you having? Commented Aug 1, 2011 at 15:57
  • 1
    So what's wrong with your code? What's result of running the code? Commented Aug 1, 2011 at 16:00
  • The wrong with my code is that i use "row[2]"to retrieve the result.But i want the program to recognize the name that i give (ex."I") and retrieve the right column from the right field . Commented Aug 1, 2011 at 16:10

2 Answers 2

2

Based on the clarification in your comment, you simply need to build your SQL statement properly:

SELECT halftime FROM RADIATION WHERE name_id = 'Tc'

Will give the result as:

6

For a more complex answer:

std::string column;
std::string name;

// have the user tell you what columns are wanted
cin >> column;

// have the user give you the criteria
cin >> name;

// build the SQL
std::stringstream sql;

sql << "SELECT " << column << " FROM RADIATION WHERE name = '" << name << "'";
Sign up to request clarification or add additional context in comments.

6 Comments

:Hello, i want the user to enter a name (for ex."I") and then get the result i want.Also,not necessary halftime.Please,see above what i want.
I've updated my answer. What you want to do is not difficult, you just have to build the SQL string properly.
:1) In the string column i write for ex. "halftime" and in "name" i write "I".How i get the results?I mean ,if i try "cout<<sql"it gives me an address. 2) I don't want the user to enter "column".I want to just enter "I" and the program gets (uses)the halftime for example.Thank you!
If halftime is only the example, how do you know what column(s) you want to display? cout << sql.str(); will display the contents of your SQL string. You have to pass this string to your mysql database.
As i said before,i want the user to input a name(ex."I") and retrieve from the database the "halftime"in order to show the result (or make calculations with "halftime").If i do "cout <<sql.str()"it doesn't show the value of halftime ,just the name halftime.I hope i am telling it right in order to understand.
|
0

This may help. You just have to process the result set that you get after executing your query.

res = stmt->executeQuery("SELECT * FROM radiation");
while (res->next()) {
  // You can use either numeric offsets...
  cout << "name_id = " << res->getInt(1); // getInt(1)/getString() returns the first column
      }

1 Comment

@:Hello,i can't figure how to do it because i am getting a lot of errors.Do you have in mind another tutorial for that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.