從PostgreSQL數據庫中獲取數據我有PostgreSQL中的數據庫。我有SQL查詢(在PostgreSQL的BTW的偉大工程,所以SQL代碼是沒有錯的):壞投同時使用SOCI
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.The查詢返回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」
什麼()的std :: bad_cast中斷(核心轉儲)
請幫幫忙,當編譯罰款我真的不知道如何解決這個問題。
也許問題是,CREATION_DATE是DATE和TM一直也時刻......?如果是這樣,如何解決這個問題?
您是否嘗試過在一個調試器中運行可執行文件? – cdhowie 2012-07-23 14:05:16
@cdhowie:不,我沒有 - 我不知道很多有關調試遺憾的是:/ – 2012-07-23 14:08:19
這很可能是一個很好的學習方式。 – cdhowie 2012-07-23 14:08:43