2012-03-15 3 views
0

설명서를 읽었으며 예제를 살펴 봤지만 C++의 전역 변수에 테이블을 할당하는 방법을 여전히 이해할 수 없습니다. 나는 조금 부끄러 웠을지도 모른다. 파이썬에서 왔고 전역 변수에 테이블을 할당하는 것은 mysqldb를 사용하는 단순한 문제입니다.mysql ++ 전역 테이블 변수

테이블을 mysqlpp 클래스 외부에서 액세스 할 수있는 전역 변수에 할당 할 수 있습니까? 내가 대신 다시 클래스로 for 루프를 이동하면 '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"); 
    } 
    } 

} 

에 선언되지 않은 : 오류 : 다음 코드를 컴파일 할 때 예를 들어

는, 나는 오류 모든 것이 잘 작동하지만 이것은 제한적입니다. 이것을 할 수있는 유일한 방법입니까? 모든 예가 그렇게 보일 것 같습니다.

+0

어떤 문서 (좋은 링크가 좋을까요). –

+0

SSQLS를 통해 이루어졌지만 [documentation] (http://www.tangentsoft.net/mysql++/doc/html/userman/ssqls.html)의 5 장을 보면 알 수 있습니다. 테이블의 각 SQL 열에 대해 데이터 멤버를 정의해야합니까? 아니면 요청하고 싶은 SQL 멤버 만 정의해야합니까? 문서는 테이블의 각 열에 대해 하나씩 정의해야하는 것처럼 들립니다. "각 SQL 열에 대해 동일한 이름을 사용하는 데이터 멤버가 있습니다." – nomadicME

+1

'try {}'블록 안에'mysqlpp :: StoreQueryResult sched_recs'를 선언 했으므로 블록이 끝나면 변수는 범위를 벗어나 try 블록 외부에서 사용할 수 없습니다. 'try {}'블록 안에'for' 루프를 옮기거나'try {}'블록 앞에 변수 선언을 옮깁니다. – Yaniro

답변

0

야니로 (Yaniro)가 제안했습니다.

이 샘플 당신이 게시 한의 유일한 차이점은 sched_recs라는 mysqlpp::StoreQueryResult 변수의 선언의 위치 코드

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

} 

참고 다음 코드를 사용해보십시오.

이 샘플에서는 try 블록의 범위가 아닌 main의 범위에서 선언되므로 try 블록이 끝난 후에도 변수의 값이 계속 유효합니다.

try 및 catch 블록 범위에 대한 자세한 내용은 Exception dangers and downsides을 참조하십시오.

+0

@yaniro와 Appleman1234에 감사드립니다. 그건 잘된거야. SSQLS는 내가 필요한 것보다 훨씬 많이 보였습니다. Try/catch 동작은 변수 범위와 관련하여 파이썬에서 try/except와 매우 다릅니다. 다시 한번 감사드립니다. – nomadicME