【问题标题】:Poco::Data::Keywords::use not working for PostgreSQL?Poco::Data::Keywords::use 不适用于 PostgreSQL?
【发布时间】:2022-12-15 03:58:31
【问题描述】:

我正在尝试使用 POCO 在 PostgreSQL 中插入值。我正在尝试使用 Poco::Data::Keywords::use 来做到这一点:

try
{
        Poco::Data::PostgreSQL::Connector::registerConnector();

        Poco::Data::Session session(Poco::Data::PostgreSQL::Connector::KEY,
                                    "host=localhost port=5433 user=test password=test dbname=test_db");

        int test1_id = 1;
        int test2_id = 2;
        int id = 0;

        session << "insert into test (test1_id, test2_id) values (?, ?) returning id",
                Poco::Data::Keywords::use(test1_id),
                Poco::Data::Keywords::use(test2_id),
                Poco::Data::Keywords::into(id),
                Poco::Data::Keywords::now;

        std::cout << "id = " << id << std::endl;
}
catch (const Poco::Exception& ex)
{
    std::cout << "exception: " << ex.displayText() << std::endl;
}

但我遇到了一个例外:

exception: PostgreSQL: [PostgreSQL]: postgresql_stmt_prepare error: ERROR:  syntax error at or near ","
LINE 1: ... into test (test1_id, test2_id) values (?, ?) retur...
                                                             ^
 insert into test (test1_id, test2_id) values (?, ?) returning id

如果我以其他方式插入:

session << "insert into presentation (test1_id, test2_id) values ('" 
    << test1_id << "', '" << test2_id << "') returning id",
    Poco::Data::Keywords::into(id),
    Poco::Data::Keywords::now;

它工作正常。第一种方法我做错了什么?

【问题讨论】:

    标签: poco-libraries


    【解决方案1】:

    作品

     session << "insert into test (test1_id, test2_id) values ($1, $2) returning id",
                    Poco::Data::Keywords::use(test1_id),
                    Poco::Data::Keywords::use(test2_id),
                    Poco::Data::Keywords::into(id),
                    Poco::Data::Keywords::now;
    

    【讨论】:

      【解决方案2】:

      太疯狂了,POCO 没有为 PostgreSQL 占位符提供任何指南。

      Here解决方案

      基本上:使用 $<place_holder_index> 而不是 '?'

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-12
        • 2019-05-15
        • 1970-01-01
        • 2020-05-09
        • 1970-01-01
        • 2012-04-14
        • 2012-05-13
        • 1970-01-01
        相关资源
        最近更新 更多