0

ドキュメントを読み、例を見ましたが、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 ループをクラスに戻すと、すべて正常に機能しますが、これは制限です。これが唯一の方法ですか?すべての例がそう思わせるようです。

4

1 に答える 1

0

ヤニロが提案したように。

次のコード スニペットを試してください

#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

このサンプルでは、​​ try ブロックのスコープではなく main のスコープで宣言されているため、変数の値は try ブロックの終了後もスコープ内にあります。

try および catch ブロックのスコープの詳細については、「例外の危険性と欠点」を参照してください。

于 2012-03-15T12:12:28.570 に答える