Barra laterale

programmazione:libpqxx:introduzione

Introduzione a libpqxx

Autore: Fabio Di Matteo
Ultima revisione: 24/02/2018 - 12:21

Select

#include <iostream>
#include <pqxx/pqxx>
 
 
 
 
int main(int, char *argv[])
{
  try
  {
	pqxx::connection c{"user=fabio password=fabio dbname=test hostaddr=192.168.1.4 port=5432"};
	pqxx::work txn{c};
 
	pqxx::result r = txn.exec("SELECT * from persone;");
	for (auto row: r)
		std::cout << 
		row["id"].c_str() <<" "<<
		row["nome"].c_str() <<" "<<
	    row["cognome"].c_str() <<" "<<
	    row["email"].c_str() << 
	    std::endl;
 
	txn.commit();
  }
  catch (const pqxx::sql_error &e)
  {
    std::cerr << "SQL error: " << e.what() << std::endl;
    std::cerr << "Query was: " << e.query() << std::endl;
  }
  catch (const std::exception &e)
  {
    std::cerr << "Error: " << e.what() << std::endl;
  }
}

Insert

#include <iostream>
#include <pqxx/pqxx>
 
 
 
 
int main(int, char *argv[])
{
  try
  {
	pqxx::connection c{"user=fabio password=fabio dbname=test hostaddr=192.168.1.4 port=5432"};
	pqxx::work txn{c};
 
	pqxx::result r = txn.exec("insert into persone (nome,cognome,email) values ('C++', 'Libpqxx','libpqxx@local.com');");
	txn.commit();
  }
  catch (const pqxx::sql_error &e)
  {
    std::cerr << "SQL error: " << e.what() << std::endl;
    std::cerr << "Query was: " << e.query() << std::endl;
  }
  catch (const std::exception &e)
  {
    std::cerr << "Error: " << e.what() << std::endl;
  }
}

makefile

all:
	g++ main.cpp `pkg-config --libs --cflags libpqxx` -o pq

programmazione/libpqxx/introduzione.txt · Ultima modifica: 18/04/2018 - 15:49 (modifica esterna)