Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8265353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:46:12+00:00 2026-06-08T04:46:12+00:00

I have a database in PostgreSQL. And I have sql query (which btw works

  • 0

I have a database in PostgreSQL. And I have sql query (which btw works great in PostgreSQL, so the sql code isn’t wrong):

SELECT COUNT(*) as size, creation_date FROM item INNER JOIN raf_item USING (id) INNER JOIN item_detail USING (id) GROUP BY creation_date;

where creation date is defined as creation_date Date; in PostgreSQL.The query returns, for example (depends on what I have in my database):

size | creation_date
21   | 12-31-2012
18   | 04-03-2002

I’m using SOCI + C++ to get data from this query. My whole C++ code:

#include <iostream>
#include <cstdlib>
#include <soci.h>
#include <string>
#include <postgresql/soci-postgresql.h>
using namespace std;

bool connectToDatabase(soci::session &sql, string databaseName, string user, string password)
{
    try
    {
        sql.open(soci::postgresql, "dbname=" +databaseName + " user="+user + " password="+password);
    }
    catch (soci::postgresql_soci_error const & e)
    {
        cerr << "PostgreSQL error: " << e.sqlstate() << " " << e.what() << std::endl;
        return false;
    }
    catch (std::exception const & e)
    {
        cerr << "Some other error: " << e.what() << std::endl;
        return false;
    }
    return true;
}

void getDataFromDatabase(soci::session &sql)
{
    soci::row r;
    sql << "select count(*) as size, creation_date from item inner join raf_item using (id) inner join item_detail using (id) group by creation_date;", soci::into(r);
    for(std::size_t i = 0; i != r.size(); ++i)
    {
        cout << r.get<int>(i);
        tm when = r.get<tm>(i);
        cout << asctime(&when);
    }
}

int main(int argc, char **argv)
{
    soci::session sql;
    bool success = connectToDatabase(sql, "testdb", "testuser", "pass");
    if (success)
    {
        cout << "Connected.\n";
        getDataFromDatabase(sql);
    }

    else
        cout << "Not connected.\n";
    return 0;
}

But I got this error, when I tried to run the application (compilation is fine) :

terminate called after throwing an instance of ‘std::bad_cast’
what(): std::bad_cast Interrupt (core dumped)

Please help, when the compilation is fine I really don’t know how to fix this.

Maybe the problem is that creation_date is DATE and tm keeps also time … ? If so, how to fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:46:14+00:00Added an answer on June 8, 2026 at 4:46 am

    While you did solve your question, the code you posted is more a workaround than the real solution for the problem.

    Your problem is, that COUNT(*) returns a value of bigint (or int8) type, as described here,
    and soci converts bigint to a long long int type, as described in this chart. If the types do not match excatly, a bad_cast exception will be thrown.

    Therefore, the code in your question should be cout << r.get<long long>(i); to avoid the bad_cast exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given SQL as an input, I have to query a PostgreSQL database and return
I have the below query in a postgresql database SELECT * FROM accounts where
I have a problem with an SQL query on Postgresql. This select clause is
I have a postgresql database and am using it for a project which handles
I have an SQL query in PostgreSQL that I'd like to translate to Django.
I have a PostgreSQL 9.1 database in which pictures are stored as large objects.
I have the following query which runs perfectly well on both Oracle and SQL
I have a piece of code that works in a background process which looks
We have a web application talking to a Postgres SQL database at work -
I am lost. I have localhost database (PostgreSQL) and I have to add port

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.