Domanda

Ho letto attraverso la documentazione e ha esaminato gli esempi, ma non riesco ancora a capire come assegnare una tabella a una variabile globale in C ++.Potrei essere un po 'viziato.Venendo da Python e assegni un tavolo a una variabile globale è davvero una questione semplice usando mysqldb.

Una tabella può essere assegnata a una variabile globale da accedere all'esterno della classe MySQLPP?

Come esempio, quando viene compilato il seguente codice, ottengo l'errore: Errore: 'Sched_recs' non è stato dichiarato in questo scopo

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

}
.

Se invece spostassi il ciclo per tornare alla classe, allora tutto funziona bene, ma questo è limitante.È questo l'unico modo per farlo?Tutti gli esempi sembrano farlo sembrare così.

È stato utile?

Soluzione

Come suggeriva Yaniro.

Prova il seguente snippet di codice

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

}
.

Nota che solo la differenza in questo campione e quella che hai pubblicato è la posizione della dichiarazione della variabile mysqlpp::StoreQueryResult chiamata sched_recs.

In questo campione è dichiarato nello scopo del principale, piuttosto che nello scopo del blocco Try, quindi il valore della variabile è ancora in ambito dopo che il blocco prove finisce.

Per ulteriori informazioni su Prova e Catch Block Scope See EccezionePericoli e dinossi .

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top