2012-03-15 104 views
0

我已經通讀了文檔,並看了一些示例,但我仍然無法弄清楚如何將表分配給C++中的全局變量。我可能會有點寵壞。我來自python,並將表分配給全局變量,使用mysqldb確實很簡單。mysql ++全局表變量

是否可以將表分配給全局變量以便在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"); 
    } 
    } 

} 

未聲明如果我不是移動迴環進級,那麼一切正常,但這是限制。這是唯一的方法嗎?所有的例子似乎都使它看起來如此。

+0

什麼文件(鏈接會很好)。 – 2012-03-15 06:10:02

+0

聽起來好像是通過SSQLS完成的,但是查看[documentation](http://www.tangentsoft.net/mysql++/doc/html/userman/ssqls.html)的第5章。我真的必須爲表中的每個SQL列定義一個數據成員,還是隻需要爲我想要請求的數據成員定義一個數據成員?這些文檔讓我聽起來像是必須爲表中的每一列定義一個:「其中每個SQL列有一個數據成員,使用相同的名稱」。 – nomadicME 2012-03-15 08:52:16

+1

請注意,您已在'try {}'塊內聲明瞭'mysqlpp :: StoreQueryResult sched_recs',因此當該塊結束時,該變量超出範圍,並且不能在try塊之外使用。在'try {}'塊內移動'for'循環或者在'try {}'塊之前移動變量聲明。 – Yaniro 2012-03-15 10:30:28

回答

0

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

} 

注意下面的代碼片段,只有這個樣品和您發佈的一箇中差被稱爲sched_recsmysqlpp::StoreQueryResult變量的聲明的位置。

在本示例中,它在main範圍內聲明,而不是try塊的範圍,所以在try塊完成後變量的值仍然在範圍內。請參閱Exception dangers and downsides

+0

謝謝@yaniro和Appleman1234的幫助。這很好。真是一種解脫,SSQLS的東西看起來比我所需要的要多得多。 try/catch行爲,就變量範圍而言,與try/python大相徑庭。再次感謝。 – nomadicME 2012-03-15 17:38:36