【问题标题】:Create dynamically-named SQL table with POCO使用 POCO 创建动态命名的 SQL 表
【发布时间】:2016-09-06 20:24:43
【问题描述】:

以下代码在 POCO 中创建了一个名为“test”的表:

#include "Poco/Data/Session.h"
#include "Poco/Data/SQLite/Connector.h"

#include <string>

using namespace Poco::Data::Keywords;
using Poco::Data::Session;

int main(int argc, char** argv)
{
    Poco::Data::SQLite::Connector::registerConnector();
    Session session("SQLite", "test.db");
    session << "CREATE TABLE test (id INTEGER)", now;
    return 0;
}

我想编写一个函数来创建根据给定参数命名的表。我本来想用

int main(int argc, char** argv)
{
    Poco::Data::SQLite::Connector::registerConnector();
    Session session("SQLite", "test.db");
    std::string name = "test";
    session << "CREATE TABLE ? (id INTEGER)", bind(name), now;
    return 0;
}

使用通配符并将其替换为给定名称。据我了解,我什至不需要bind(它复制给定值)和use 应该就足够了,因为该语句以now 终止,但没关系。

无论我使用哪个关键字(usebinduseRef),程序都会抛出“SQL 错误或缺少数据库”错误。

我也尝试过创建 Statement 并让它创建表,但没有任何变化。

我做错了吗?此调用不允许使用通配符替换吗?我必须手动修改调用吗?

如果它是相关的,可以从我的#includes 推断,我正在使用 SQLite。

【问题讨论】:

    标签: c++ sqlite poco-libraries


    【解决方案1】:

    想通了。

    在这里使用printf 格式说明符有效。所以声明应该是

    int main(int argc, char** argv)
    {
        Poco::Data::SQLite::Connector::registerConnector();
        Session session("SQLite", "test.db");
        std::string name = "test";
        session << "CREATE TABLE %s (id INTEGER)", name, now;
        return 0;
    }
    

    这需要在每次调用它时重新编译语句,所以我不确定为什么这是必要的(如果我想创建多个类似的表怎么办?),但是c'est la vie

    【讨论】:

      猜你喜欢
      • 2011-02-23
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      相关资源
      最近更新 更多