0

簡単なプログラムをコンパイルする方法を見つけたとき、別の問題が発生しました... PostgreSQL をインストールし、データベースとテーブルを作成しました。

1) createdb testDB 2) テーブル都市を作成します (city varchar(80), location varchar(80));

そして、私のまだ非常に単純なプログラム:

#include <iostream>
#include <soci.h>
#include <postgresql/soci-postgresql.h>
#include <string>
using namespace std;

int main(int argc, char **argv)
{
   try
   { 
      soci::session sql(soci::postgresql, "dbname=testDB");

    string row = "";
    sql << "select * from cities;", soci::into(row);

    sql << "insert into cities values('London', 'UK')";

    sql << "select * from cities;", soci::into(row);
    cout << row << "\n"; 
   }
   catch (soci::postgresql_soci_error const & e)
   {
      std::cerr << "PostgreSQL error: " << e.sqlstate() << " " << e.what() << std::endl;
   }
   catch (std::exception const & e)
   {
      std::cerr << "Some other error: " << e.what() << std::endl;
   }
   return 0;

}

このコードは、testDB に既にある行のみを表示し、挿入したばかりの行は表示しません。例: 私のtestDBのテーブルcityには、次のものがあります:

ワルシャワ ポーランド ベルリン ドイツ パリ フランス

上記のコードは私を示しています:

ワルシャワ、ポーランド

しかし、表示されません:

ベルリン ドイツ パリ フランス ロンドン イギリス

助けてください:(

4

1 に答える 1

2

したがって、sql << "insert into cities values ('London', 'UK')"; この問題を解決した後に commit を追加します。

于 2012-07-11T16:04:25.657 に答える