0

The following code creates a table named "test" in POCO:

#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;
}

I want to write a function which creates tables named according to the given arguments. I would have thought to use

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;
}

Using the wildcard character and replacing it with the given name. As I understand it, I shouldn't even need bind (which copies the given value) and use should be sufficient since the statement is terminated with now, but no matter.

Regardless of which keyword I use (use, bind, useRef), the program throws an "SQL error or missing database" error.

I've also tried creating a Statement and have it create the table, but no change.

Am I doing something wrong? Is wildcard substitution not allowed with this call? Do I have to modify the call manually?

Should it be relevant, as can be inferred from my #includes, I'm using SQLite.

1 Answer 1

0

Figured it out.

Using the printf format specifiers works here. So the statement should be

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;
}

This requires recompiling the statement every time it's called, so I'm not sure why this is necessary (what if I wanted to create multiple similar tables?), but c'est la vie.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.