簡體   English   中英

使用SOCI從PostgreSQL數據庫獲取數據時投射錯誤

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

我在PostgreSQL中有一個數據庫。 而且我有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;

其中創建日期定義為creation_date Date; 在PostgreSQL中。例如,查詢返回(取決於我在數據庫中擁有的內容):

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

我正在使用SOCI + C ++從此查詢中獲取數據。 我的整個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;
}

但是,當我嘗試運行該應用程序時,出現了此錯誤(編譯很好):

拋出'std :: bad_cast'實例后終止調用
what():std :: bad_cast中斷(核心已轉儲)

請幫助,當編譯正常時,我真的不知道該如何解決。

也許問題在於creation_date是DATE,而tm也保留了時間...? 如果是這樣,該如何解決?

當您確實解決了問題時,發布的代碼比真正的問題解決方案更具解決方法。

您的問題是,COUNT(*)返回BIGINT(或INT8)的值輸入,如所描述這里 ,和SOCI轉換BIGINT到long long int類型,如所描述的在該圖表中 如果類型不完全匹配,則將引發bad_cast異常。

因此,您問題中的代碼應為cout << r.get<long long>(i); 以避免bad_cast異常。

好的,我自己解決了!:)

繼承人實際上可以正常工作的代碼(我只用下面的代碼補充了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