2012-03-15 8 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

どのようなドキュメント(リンクがいいですか)。 –

+0

これはSSQLS経由で行われているようですが、[documentation](http://www.tangentsoft.net/mysql++/doc/html/userman/ssqls.html)の第5章を参照してください。実際には、テーブル内の各SQLカラム、または要求したいカラムだけにデータメンバーを定義する必要がありますか?ドキュメントでは、テーブルの各列に1つずつ定義する必要があるように、「同じ名前を使用して各SQL列のデータメンバーがあります」というように音が鳴ります。 – nomadicME

+1

'mysqlpp :: StoreQueryResult sched_recs'を' try {} 'ブロック内で宣言しているので、ブロックが終了すると変数はスコープから外れ、tryブロックの外側では使用できません。 'try {}'ブロックの中で 'for'ループを動かすか、' try {} 'ブロックの前に変数宣言を動かしてください。 – Yaniro

答えて

0

ヤニロとして提案。

は、このサンプル、あなたが掲示1の唯一の違いは、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の振る舞いは、変数スコープに関しては、Pythonのtry/exceptとは大きく異なります。再度、感謝します。 – nomadicME