Question

I have read through the documentation, and looked at the examples, but I still can't figure out how to assign a table to a global variable in C++. I might be a little spoiled. I'm coming from python and assigning a table to a global variable is really a simple matter using mysqldb.

Can a table be assigned to a global variable to be accessed outside the mysqlpp class?

As an example, when the following code is compiled, I get the error: error: ‘sched_recs’ was not declared in this scope

#include <mysql++.h>
#include <string>
#include <time.h>

using namespace std;
using namespace mysqlpp;

int SSE (const char* timeStr)
{
  time_t sse;
  struct tm tm;
  strptime(timeStr, "%Y-%m-%d %H:%M:%S", &tm);
  sse = mktime(&tm);
  return (sse);
}

int main()
{
  string table = "sched_recs";
  time_t now, tt; 
  now = time(NULL);

  try
  {
  // Connect to the sample database.
  mysqlpp::Connection conn(false);
  //             databasename,    host,   username, password
  if (conn.connect("dbname", "dbhost", "dbusername", "dbpasswd")) {
      // Retrieve a subset of the sample stock table set up by resetdb
      // and display it.
      //stmt = "select datetime from %s", table 
      mysqlpp::Query query = conn.query("select * from sched_recs");
      mysqlpp::StoreQueryResult sched_recs = query.store();
      //if (mysqlpp::StoreQueryResult tableData = query.store()) {
          ////cout << "We have:" << endl;

      //}
  }
  }
  catch (Exception& e)
  {
    cerr << "Exception: " << e.what() << endl;
  }

  for (size_t i = 0; i < sched_recs.num_rows(); ++i) {
    if (SSE(sched_recs[i][1]) - 60 * 3 < now && SSE(sched_recs[i][2]) > now)
    {
      cout << '\t' << sched_recs[i][1] << endl;
      //system("at -f test.py '18:30' today");
    }
  }

}

If I instead move the for loop back into the class, then everything works fine, but this is limiting. Is this the only way to do it? All the examples seem to make it seem so.

Was it helpful?

Solution

As Yaniro suggested.

Try the following snippet of code

#include <mysql++.h>
#include <string>
#include <time.h>

using namespace std;
using namespace mysqlpp;

int SSE (const char* timeStr)
{
  time_t sse;
  struct tm tm;
  strptime(timeStr, "%Y-%m-%d %H:%M:%S", &tm);
  sse = mktime(&tm);
  return (sse);
}

int main()
{
  string table = "sched_recs";
  mysqlpp::StoreQueryResult sched_recs;
  time_t now, tt; 
  now = time(NULL);

  try
  {
  // Connect to the sample database.
  mysqlpp::Connection conn(false);
  //             databasename,    host,   username, password
  if (conn.connect("dbname", "dbhost", "dbusername", "dbpasswd")) {
      // Retrieve a subset of the sample stock table set up by resetdb
      // and display it.
      //stmt = "select datetime from %s", table 
      mysqlpp::Query query = conn.query("select * from sched_recs");
      sched_recs = query.store();
      //if (mysqlpp::StoreQueryResult tableData = query.store()) {
          ////cout << "We have:" << endl;

      //}
  }
  }
  catch (Exception& e)
  {
    cerr << "Exception: " << e.what() << endl;
  }

  for (size_t i = 0; i < sched_recs.num_rows(); ++i) {
    if (SSE(sched_recs[i][1]) - 60 * 3 < now && SSE(sched_recs[i][2]) > now)
    {
      cout << '\t' << sched_recs[i][1] << endl;
      //system("at -f test.py '18:30' today");
    }
  }

}

Note that only difference in this sample and the one you posted is the location of the declaration of the mysqlpp::StoreQueryResult variable called sched_recs.

In this sample it is declared in the scope of main , rather than the scope of the try block, so the variable's value is still in scope after the try block finishes.

For more information about try and catch block scope see Exception dangers and downsides.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top