Re: Sample C++ code using libpqxx/Postgresql sought - Derkeiler.com

arizonahoopleData Management

Nov 28, 2012 (4 years and 11 months ago)

316 views

Re: Sample C++ code using libpqxx/Postgresql
sought
Source: http://newsgroups.derkeiler.com/Archive/Comp/comp.sources.d/2007−01/msg00001.html
From: "Generic Usenet Account" <usenet@xxxxxxxxxxxxxxx>•
Date: 26 Jan 2007 13:01:51 −0800•
On Jan 26, 2:16 pm, use...@xxxxxxxxxxxxxxx wrote:
I am looking for some sample code using the libpqxx (C++) API for
Postgresql. I have found some tutorials
I did find some sample code at the following location:
http://thaiopensource.org/development/libpqxx/attachment/wiki/WikiStart/fosdem2003.tar.gz
accessible via
http://thaiopensource.org/devprojects/libpqxx/doc/2.6.8/html/Reference/
The sample code is reproduced below for reference:
//////////////////sample1.cxx/////////////////
#include <iostream>
#include <pqxx/connection.h>
#include <pqxx/transaction.h>
#include <pqxx/result.h>
using namespace std;
using namespace pqxx;
int main()
{
try
{
Connection C("");
Transaction T(C, "sample1");
Result R = T.Exec("SELECT tablename FROM pg_tables");
// Process each successive result tuple using iterators, converting
each
// field to a type of our choice−−in this case, a C++ string.
Re: Sample C++ code using libpqxx/Postgresql sought
Re: Sample C++ code using libpqxx/Postgresql sought 1
for (Result::const_iterator c = R.begin(); c != R.end(); ++c)
{
// Read value of column 0 into a string N
string N;
c[0].to(N);
cout << '\t' << c.num() << '\t' << N << endl;
}
cout << endl;
// Now do the same thing using array indexing, reading each field
as a raw
// C string.
for (Result::size_type i = 0; i < R.size(); ++i)
cout << '\t' << i << '\t' << R[i][0].c_str() << endl;
// Tell the transaction that it has been successful
T.Commit();
}
catch (const exception &e)
{
cerr << "Exception: " << e.what() << endl;
return 1;
}
return 0;
}
//////////////////sample2.cxx/////////////////
#include <iostream>
#include <string>
#include <pqxx/connection.h>
#include <pqxx/cursor.h>
#include <pqxx/transaction.h>
#include <pqxx/result.h>
using namespace std;
using namespace pqxx;
void DumpRows(const Result &R)
{
for (Result::const_iterator i = R.begin(); i != R.end(); ++i)
cout << "\t" << i[0].c_str() << "\t" << i[1].c_str() << endl;
cout << endl;
}
Re: Sample C++ code using libpqxx/Postgresql sought
Re: Sample C++ code using libpqxx/Postgresql sought 2
int main()
{
int Rows;
Result R;
try
{
Connection C("");
Transaction T(C, "sample2");
// Count rows; converts field from query to int
R = T.Exec("SELECT count(*) FROM events");
R.at(0).at(0).to(Rows);
cout << "Table contains " << Rows << " rows." << endl;
// Create cursor, intended to read 3 rows at a time
Cursor Cur(T, "SELECT * FROM events ORDER BY year", "tablecur", 3);
// Read batch of 3 rows into Result
Cur >> R;
DumpRows(R);
// Read arbitrary number of rows
R = Cur.Fetch(2);
DumpRows(R);
// Now let's read the same rows backwards...
// Move cursor 1 step forward to make subsequent backwards fetch
include
// current row
Cur += 1;
// Set different default batch size for cursor
Cur.SetCount(Cursor::BACKWARD_ALL());
Cur >> R;
DumpRows(R);
}
catch (const exception &e)
{
cerr << "Exception: " << e.what() << endl;
return 1;
}
return 0;
}
//////////////////sample3.cxx/////////////////
Re: Sample C++ code using libpqxx/Postgresql sought
Re: Sample C++ code using libpqxx/Postgresql sought 3
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include <pqxx/connection.h>
#include <pqxx/cursor.h>
#include <pqxx/transaction.h>
#include <pqxx/result.h>
using namespace std;
using namespace pqxx;
void DumpRows(const Result &R)
{
for (Result::const_iterator i=R.begin(); i!=R.end(); ++i)
cout << "\t" << i[0].c_str() << "\t" << i[1].c_str() << endl;
}
int main()
{
Result R;
try
{
Connection C("");
Transaction T(C, "sample3");
Cursor Cur(T, "SELECT * FROM events ORDER BY year", "tablecur", 5);
Cur >> R;
DumpRows(R);
cout << endl;
// Now read rows backwards, repositioning our cursor for each row
for (Cursor::size_type i = 4; i >= 0; −−i)
{
Cur.MoveTo(i);
DumpRows(Cur.Fetch(1));
}
}
catch (const exception &e)
{
// All exceptions thrown by libpqxx are derived from std::exception
cerr << "Exception: " << e.what() << endl;
return 2;
Re: Sample C++ code using libpqxx/Postgresql sought
Re: Sample C++ code using libpqxx/Postgresql sought 4
}
catch (...)
{
// This is really unexpected (see above)
cerr << "Unhandled exception" << endl;
return 100;
}
return 0;
}
//////////////////sample4.cxx/////////////////
#include <iostream>
#include <pqxx/connection.h>
#include <pqxx/transaction.h>
#include <pqxx/cachedresult.h>
using namespace std;
using namespace pqxx;
int main()
{
try
{
Connection C("");
Transaction T(C, "sample4");
CachedResult R(T, "SELECT * FROM events ORDER BY year", "result4",
2);
// Read query results, fetching them on demand
for (Result::size_type i = 0; i < R.size(); ++i)
cout << '\t' << R[i][0].c_str() << '\t' << R[i][1].c_str() <<
endl;
}
catch (const exception &e)
{
cerr << "Exception: " << e.what() << endl;
return 1;
}
return 0;
}
//////////////////sample5.cxx/////////////////
#include <iostream>
#include <stdexcept>
Re: Sample C++ code using libpqxx/Postgresql sought
Re: Sample C++ code using libpqxx/Postgresql sought 5
#include <pqxx/connection.h>
#include <pqxx/tablereader.h>
#include <pqxx/tablewriter.h>
#include <pqxx/transaction.h>
using namespace std;
using namespace pqxx;
class InsertRows : public Transactor
{
public:
InsertRows() : Transactor("sample5") {}
void operator()(argument_type &T)
{
T.Exec("INSERT INTO events(year, event) VALUES (2001, 'OSDEM
2001')");
T.Exec("INSERT INTO events(year, event) VALUES (2001, 'FOSDEM
2002')");
throw runtime_error("Oops, something goes wrong before we can
finish!");
T.Exec("INSERT INTO events(year, event) VALUES (2001, 'FOSDEM
2003')");
}
void OnCommit()
{
cout << "Rows inserted successfully." << endl;
}
void OnAbort(const char Reason[]) throw ()
{
cout << "No rows inserted: " << Reason << endl << endl;
}
};
int main()
{
try
{
Connection C("");
C.Perform(InsertRows());
}
catch (const exception &e)
Re: Sample C++ code using libpqxx/Postgresql sought
Re: Sample C++ code using libpqxx/Postgresql sought 6
{
cerr << "Exception: " << e.what() << endl;
return 1;
}
return 0;
}
/////////////////////////////////////////////////////////
.
Re: Sample C++ code using libpqxx/Postgresql sought
Re: Sample C++ code using libpqxx/Postgresql sought 7