简体   繁体   English

使用SOCI从PostgreSQL数据库获取数据时投射错误

[英]Bad cast while getting data from PostgreSQL database using SOCI

I have a database in PostgreSQL. 我在PostgreSQL中有一个数据库。 And I have sql query (which btw works great in PostgreSQL, so the sql code isn't wrong ): 而且我有sql查询(btw在PostgreSQL中很好用, 因此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;

where creation date is defined as creation_date Date; 其中创建日期定义为creation_date Date; in PostgreSQL.The query returns, for example (depends on what I have in my database): 在PostgreSQL中。例如,查询返回(取决于我在数据库中拥有的内容):

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

I'm using SOCI + C++ to get data from this query. 我正在使用SOCI + C ++从此查询中获取数据。 My whole C++ code: 我的整个C ++代码:

#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' 抛出'std :: bad_cast'实例后终止调用
what(): std::bad_cast Interrupt (core dumped) what():std :: bad_cast中断(核心已转储)

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 ... ? 也许问题在于creation_date是DATE,而tm也保留了时间...? If so, how to fix this? 如果是这样,该如何解决?

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 . 您的问题是,COUNT(*)返回BIGINT(或INT8)的值输入,如所描述这里 ,和SOCI转换BIGINT到long long int类型,如所描述的在该图表中 If the types do not match excatly, a bad_cast exception will be thrown. 如果类型不完全匹配,则将引发bad_cast异常。

Therefore, the code in your question should be cout << r.get<long long>(i); 因此,您问题中的代码应为cout << r.get<long long>(i); to avoid the bad_cast exception. 以避免bad_cast异常。

OK, I SOLVED IT BY MYSELF!:) 好的,我自己解决了!:)

Heres the code that actualy works fine (I only repleced getDataFromDatabase with this code below): 继承人实际上可以正常工作的代码(我只用下面的代码补充了getDataFromDatabase ):

void getDataFromDatabase(soci::session &sql)
{
    long size;
    string date;

    soci::row r;
    sql << "select count(*) as size, creation from item inner join raf_item using (id) inner join item_detail using (id) group by creation;", soci::into(r);
    for(std::size_t i = 0; i != r.size(); ++i)
    {
        const soci::column_properties & props = r.get_properties(i);

    cout << '<' << props.get_name() << '>';

    switch(props.get_data_type())
    {
    case soci::dt_string:
        cout << r.get<std::string>(i);
        break;
    case soci::dt_double:
        cout << r.get<double>(i);
        break;
    case soci::dt_integer:
        cout << r.get<int>(i);
        break;
    case soci::dt_unsigned_long:
        cout << r.get<unsigned long>(i);
        break;
    case soci::dt_long_long:
        cout << r.get<long long>(i);
        size = r.get<long long>(i);
        break;
    case soci::dt_date:
        std::tm when = r.get<std::tm>(i);
        cout << asctime(&when);
        date = asctime(&when);
        break;
    }

    cout << "\n" << size << "\n";
    cout << "\n" << date << "\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM