Pqxx tutorial

From IML Wiki
Jump to: navigation, search

Tested with PostgreSQL_13.2_64bit from https://postgrespro.com/windows

  • 1. Download 7.4.1 version of libpxx library https://github.com/jtv/libpqxx
  • 2. Use CMake to make Build for Visual Studio 2019 x64 Release.
  • 3. Run Visual Studio 2019 as administrator and open created before .sln pjoject.
  • 4. Ensure that you use last c++20 standard and x64 Release config.
  • 5. Make ALL_BUILD and then INSTALL builds. Libpqxx must be installed in C:\Program Files\libpqxx
  • 6. Create new c++ project with next code:
#pragma once

#include <string>
#include <iostream>
#include <pqxx/pqxx>

int main()
{
    std::string connectionString = "host=localhost port=5432 dbname=test user=postgres password =123454321";

    try
    {
        pqxx::connection connectionObject(connectionString.c_str());

        pqxx::work worker(connectionObject);

        pqxx::result response = worker.exec("SELECT * FROM users");

        for (size_t i = 0; i < response.size(); i++)
        {
            std::cout << "Id: " << response[i][0] << " Username: " << response[i][1] << " Password: " << response[i][2] << " Email: " << response[i][3] << std::endl;
        }
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    system("pause");
}
  • 7. Change configure to x64 Release and c++20 standard
  • 8. Add additional include directories C:\Program Files\libpqxx\include
  • 9. Add additional library directories C:\Program Files\PostgreSQL\13\lib and C:\Program Files\libpqxx\lib;
  • 10. Add additional dependencies libpq.lib; pqxx.lib; ws2_32.lib; wsock32.lib;