zamek 42
zamek 42

Reputation: 843

Postgresql libpqxx create temporary table sometimes not exists

I need to use a temporary table in my C++ app. After connect I try to do it like this:

bool DBPG::createRulesHelper()
{
    bool retVal=false;
    PGresult *res = PQexec(m_Connection, "create temporary table if not exists rules_helper(tree_instance_id numeric(38));");
    if (PQresultStatus(res) == PGRES_COMMAND_OK) {
        PQclear(res);
        res=PQexec(m_Connection,  "select count(*) from rules_helper;");
        if (PQresultStatus(res) == PGRES_TUPLES_OK) {
            LOG_I(3, "rules_helper succesfully created");
            m_RulesHelperCreated = true;
        }
        PQclear(res);
    }
    else
        PQclear(res);
}

Here is how I create connection object:

bool DBPG::Connect(const char *user, const char *password, const char *DBName)
{
    if (!(user&&*user&&password&&*password&&DBName&&*DBName)) {
        LOG_E("Connect error, user||password||DBName is null or empty");
        return false;
    }

    std::stringstream ss;
    ss << "postgresql://" << user << ":" << password <<"@" << DBName;
    std::string s=ss.str();
    const char *connInfo=s.c_str();
    m_Connection = PQconnectdb(connInfo);
    ConnStatusType st=PQstatus(m_Connection);

    if (st != CONNECTION_OK) {
        LOG_E("Connect error, status:%d", st);
        PQfinish(m_Connection);
        m_bIsConnected=m_bConnectionOk=false;
        return false;
    }

    m_bIsConnected=m_bConnectionOk=true;
    return createRulesHelper();
}

and later when I tried to use it in an sql sometimes I've got an error as table rules_helper is not exists. Do I need to wait a couple of seconds after create a temporary table? Do temporary tables belong to connection? I don't close the connection until the end of my application. thx, Zamek

Upvotes: 0

Views: 71

Answers (0)

Related Questions