문제

문서를 읽고 예제를 살펴 보았지만 C ++에서 전역 변수에 테이블을 할당하는 방법을 여전히 알아낼 수 없습니다.나는 조금 버릇 없을지도 모른다.Python에서 나와 글로벌 변수에 테이블을 할당하는 것은 정말 mysqldb를 사용하는 간단한 문제입니다.

테이블이 MySQLPP 클래스 외부에서 액세스 할 전역 변수에 할당 할 수 있습니까?

다음 코드가 컴파일 될 때 오류가 발생합니다. 오류 :이 범위에서 'sched_recs'가 선언되지 않았습니다

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

}
.

FOR LOOP를 클래스로 다시 이동하면 모든 것이 잘 작동하지만 이는 제한적입니다.이것이 그것을 할 수있는 유일한 방법입니까?모든 예제가 그렇게 보이게하는 것처럼 보입니다.

도움이 되었습니까?

해결책

yaniro가 제안했다.

다음 코드 스 니펫

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

}
.

이 샘플의 차이점과 게시 한 것은 mysqlpp::StoreQueryResult라는 sched_recs 변수의 선언 위치입니다.

이 샘플에서는 시도 블록의 범위가 아닌 메인의 범위에서 선언되므로 시도가 마침을 마친 후 변수의 값이 여전히 범위에 있습니다.

시도 및 catch 블록 범위에 대한 자세한 내용은 "nofollow"> 예외를 참조하십시오.위험과 단점 .

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top