SOCI (SQL C++ wrapper) - PostgreSQL 不执行命令 (?)

SOCI (SQL C++ wrapper) - PostgreSQL doesn't execute commands (?)

本文关键字:执行 命令 PostgreSQL C++ wrapper SOCI SQL      更新时间:2023-10-16

当我知道如何编译一个简单的程序时,现在我有其他问题。。。我安装了PostgreSQL并创建了数据库和表:

1) createdb testDB 2)创建表城市(城市varchar(80),位置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中,在表城市中,我有:

波兰华沙德国柏林巴黎-法国

上面的代码告诉我:

华沙-波兰

但不显示:

德国柏林法国巴黎伦敦英国

请帮助:(

因此,在sql << "insert into cities values ('London', 'UK')";之后添加提交解决这个问题。