Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// PgTest.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "libpq-fe.h"
#include <iostream>
#include <WinSock2.h>
#include <time.h>
#include <string.h>
#include <sstream>
#include <vector>
using namespace std;
struct Track {
int planeId;
long long timestamp;
vector<double> data;
};
double double_swap(double d)
{
union
{
double d;
unsigned char bytes[8];
} src, dest;
src.d = d;
dest.bytes[0] = src.bytes[7];
dest.bytes[1] = src.bytes[6];
dest.bytes[2] = src.bytes[5];
dest.bytes[3] = src.bytes[4];
dest.bytes[4] = src.bytes[3];
dest.bytes[5] = src.bytes[2];
dest.bytes[6] = src.bytes[1];
dest.bytes[7] = src.bytes[0];
return dest.d;
}
int main()
{
PGconn *conn = PQconnectdb("user=postgres password=admin dbname=fuseval hostaddr=127.0.0.1 port=5432");
if (PQstatus(conn) != CONNECTION_OK)
{
printf("Connection to database failed");
}
int handle1 = PQsendQueryParams(conn, "SELECT * FROM real_data where plane_id = 1", NULL, NULL, NULL, NULL, NULL, 1);
int handle2 = PQsetSingleRowMode(conn);
vector<Track> tracks;
PGresult *res;
while (res = PQgetResult(conn))
{
if (PQresultStatus(res) == PGRES_SINGLE_TUPLE) {
char *planeIdPtr = PQgetvalue(res, 0, 0);
char *timestampPtr = PQgetvalue(res, 0, 1);
Track track;
track.planeId = ntohl(*((uint32_t *)planeIdPtr));
track.timestamp = ntohll(*((long long *)timestampPtr));
for (int j = 0; j < 120; j++)
{
char* dataPtr = PQgetvalue(res, 0, j + 2);
track.data[j] = double_swap(*(double *)dataPtr);
}
tracks.push_back(track);
}
PQclear(res);
}
string stop = "stop";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.