Coming from Windows background I was a bit worried about the complexity of accessing Postgres databases from C++. I'm still having nightmares of early ODBC programming back in the 90ties. But as a mature developer I quickly overcame Microsoft's fixation on building the mother of all DBMS access technologies; that is - a common database mechanism to access various DBMSes.

Seriously, how many times in life have you ported a database from MS SQL Server to Oracle? There is a brilliang library for accessing Postgres (and only Postgres!) from C++ available. It is pqxx. It comes with a tutorial to help you getting started.

Here is Hello World of libpqxx. It doesn't get any simpler then that. Following code executes a query on a database.

  1. #include <pqxx/pqxx>  
  2.   
  3. using namespace pqxx;  
  4.   
  5. void execute_query() {  
  6.     connection cn("dbname=my_database");  
  7.     work w(cn, "mytransaction");  
  8.     w.exec("INSERT INTO city(city_name) VALUES ('Ljubljana');");  
  9.     w.commit();  
  10. }  

1 comment(s) :

I think your blog post is the only example of what include file is needed to get started. I can not find the include in any of the examples that come with the library or the tutorial you link to. Anyway. Thanks!

4:28 PM  

Newer Post Older Post Home

Blogger Syntax Highliter