mySQL with C++

Hi guys,

I'm experiencing a problem in my project now.

How do i actually get a value from my database and insert the data into a variable in my program?

char filter_exp[] = "";

MYSQL *conn; // pointer to MySQL structure

conn = mysql_init(NULL); // connection handle



// Connect to MySQL database
// host address = localhost, username = root, database = project

mysql_real_connect(conn, "localhost", "root", "", "project", 0, NULL, 0);
mysql_query(conn, "SELECT Filter from setting where transmit='1'");



I need to get a value from the table setting , field = Filter and get the value inside. How do i assign a variable now? i really don't get it. Please help.
The C API documentation is on MySQL's website. I usually only mess with MySQL in PHP but this should help to start you in the right direction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
MYSQL *conn =  0;
MYSQL_RES *result = 0;

conn = mysql_init(conn);
conn = mysql_real_connect(conn,"localhost","root","","project",0,0,0);
if(conn)
{
  if(mysql_query(conn,"SELECT Filter FROM setting WHERE transmit='1'")
  {
    //query failed
  } else {
    result = mysql_store_result(conn); // store the result of the query
    if(result)
    {
      unsigned int rows = mysql_num_rows(result); // number of rows in result set
      // parse results...
    } else {
      // empty result set or error
    }
    mysql_free_result(result); // make sure you call this to free up the memory allocated for the query result
  }
  mysql_close(conn);
}


EDIT: I have little C++ experience with MySQL I usually use PHP but looked at the API and gave it a shot
Last edited on
Topic archived. No new replies allowed.