【发布时间】:2012-07-21 18:45:43
【问题描述】:
我在 PostgreSQL 中有一个数据库。而且我有 sql 查询(顺便说一句,在 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;
其中创建日期在 PostgreSQL 中定义为 creation_date Date;。查询返回,例如(取决于我的数据库中的内容):
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 也保持时间...?如果是这样,如何解决这个问题?
【问题讨论】:
-
您是否尝试过在调试器中运行您的可执行文件?
-
@cdhowie:不,我没有 - 不幸的是,我对调试了解不多:/
-
这可能是一个很好的学习方式。
-
@cdhowie:我知道你是对的,我只是想也许有人对 soci 有这个问题并且可以提供帮助......我几乎可以肯定问题出在
creation_date类型...... -
那么,您尝试过什么来确认这一点?您是否尝试向
cout写信以验证崩溃发生的确切位置?看起来您收到了这次崩溃,然后就放弃了。至少要弄清楚是哪条线路触发了崩溃。
标签: c++ postgresql postgresql-9.1 soci