Pregunta

He leído a través de la documentación, y he examinado los ejemplos, pero aún no puedo averiguar cómo asignar una tabla a una variable global en C ++.Podría estar un poco echado a perder.Vengo de Python y a la asignación de una tabla a una variable global es realmente un asunto simple usando MySQLDB.

¿Puede asignarse una tabla a una variable global a que se acceda a la clase MySQLPP?

Como ejemplo, cuando se compila el siguiente código, obtengo el error: Error: 'Sched_Recs' no fue declarado en este ámbito

#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");
    }
  }

}

Si en su lugar, en su lugar, vuelva a la bucle en la clase, entonces todo funciona bien, pero esto es limitante.¿Es esta la única manera de hacerlo?Todos los ejemplos parecen hacerlo parecerlo.

¿Fue útil?

Solución

Como sugirió Yaniro.

Pruebe el siguiente fragmento de código

#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");
    }
  }

}

Tenga en cuenta que solo la diferencia en esta muestra y la que publicó es la ubicación de la Declaración de la variable de mysqlpp::StoreQueryResult llamada sched_recs.

En esta muestra se declara en el alcance de la principal, en lugar del alcance del bloque de prueba, por lo que el valor de la variable aún está en alcance después de que finalice el bloque de intentos.

Para obtener más información sobre el ámbito de Try and Catch Block, consulte excepciónPELIGROS Y DOWNSIDES .

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top