Frage

Ich habe die Dokumentation gelesen und mir die Beispiele angesehen, kann aber immer noch nicht herausfinden, wie man einer globalen Variablen in C ++ eine Tabelle zuweist.Ich könnte ein bisschen verwöhnt sein.Ich komme aus Python und das Zuweisen einer Tabelle zu einer globalen Variablen ist mit mysqldb wirklich eine einfache Sache.

Kann eine Tabelle einer globalen Variablen zugewiesen werden, auf die außerhalb der mysqlpp-Klasse zugegriffen werden kann?

Wenn zum Beispiel der folgende Code kompiliert wird, erhalte ich den Fehler:Fehlermeldung:'sched_recs' wurde in diesem Bereich nicht deklariert

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

}

Wenn ich stattdessen die for-Schleife zurück in die Klasse verschiebe, funktioniert alles einwandfrei, aber das ist einschränkend.Ist das der einzige Weg, es zu tun?Alle Beispiele scheinen es so erscheinen zu lassen.

War es hilfreich?

Lösung

Wie Yaniro vorgeschlagen hat.

Probieren Sie den folgenden Codeausschnitt aus

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

}

Beachten Sie, dass der einzige Unterschied in diesem Beispiel und dem von Ihnen geposteten der Ort der Deklaration des mysqlpp::StoreQueryResult aufgerufene Variable sched_recs.

In diesem Beispiel wird es im Gültigkeitsbereich von main und nicht im Gültigkeitsbereich des try-Blocks deklariert, sodass der Wert der Variablen nach Abschluss des try-Blocks noch im Gültigkeitsbereich ist.

Weitere Informationen zum Try- und Catch-Blockbereich finden Sie unter Ausnahme Gefahren und Nachteile.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top