powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / с++ выбор либы
3 сообщений из 3, страница 1 из 1
с++ выбор либы
    #38773732
despair1
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
какую с++ либу лучше выбрать для работы с пгскл?

скиньте плиз если не трудно, пример кода, вставляющий за 1 запрос несколько строк в таблицу
...
Рейтинг: 0 / 0
с++ выбор либы
    #38773916
drsm
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
...
Рейтинг: 0 / 0
с++ выбор либы
    #38776465
despair1
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
фух, вернемся к нашим баранам:

despair1
> скиньте плиз если не трудно, пример кода, вставляющий за 1 запрос несколько
> строк в таблицу

библиотека оказалась документирована чуть более чем никак, с трудом нашел примеры:
http://stderr.org/doc/libpqxx-dev/examples/ под какую-то старую версию pqxx

несколько раз скопипастил, маленько дописал и вроде заработало:

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
#include <pqxx/pqxx>
#include <pqxx/except.hxx>
#include <iostream>
#include <vector>
using std::cerr;
using std::endl;
using std::cout;
int table_writer(const std::string& conn_name) {
	try {
		std::stringstream s("");
		s<<"dbname="<<conn_name;
		pqxx::connection c(s.str());
		cout << "Connected to database." << endl
		         << "Backend version: " << c.server_version() << endl
			 << "Protocol version: " << c.protocol_version() << endl;
		// Begin a transaction acting on our current connection.  Give it a human-
		// readable name so the library can include it in error messages.
		pqxx::work T(c, "test2");
		pqxx::result R1( T.exec("CREATE TABLE IF NOT EXISTS test1 (id INTEGER)") );
		// Perform a query on the database, storing result tuples in R.
		//pqxx::result R( T.exec("CREATE TABLE test1 (id INTEGER)") );
		pqxx::tablewriter W(T, "test1");
		// We're expecting to find some tables...
		//if (R.empty()) throw std::logic_error("No tables found!");
		// Process each successive result tuple

		std::vector<int> MoreData;
		MoreData.push_back(3);
		    //MoreData.push_back(4);
		    W.insert(MoreData);
		W.complete();
		pqxx::result R( T.exec("SELECT * FROM test1 ") );
		for (pqxx::result::const_iterator c1 = R.begin(); c1 != R.end(); ++c1)
		{
		  // Dump tuple number and column 0 value to cout.  Read the value using
		  // as(), which converts the field to the same type as the default value
		  // you give it (or returns the default value if the field is null).
		  cout << '\t' << pqxx::to_string(c1.num()) << '\t' << c1[0].as(std::string()) << endl;
		}

		// Tell the transaction that it has been successful.  This is not really
		// necessary here, since we didn't make any modifications to the database
		// so there are no changes to commit.
		T.commit();

	}
	catch (const pqxx::sql_error &e)
	  {
	    // The sql_error exception class gives us some extra information
	    cerr << "SQL error: " << e.what() << endl
	         << "Query was: " << e.query() << endl;
	    return 1;
	  }
	 catch (const std::exception &e)
	  {
	    // All exceptions thrown by libpqxx are derived from std::exception
	    cerr << "Exception: " << e.what() << endl;
	    return 2;
	  }
	 catch (...)
	  {
	    // This is really unexpected (see above)
	    cerr << "Unhandled exception" << endl;
	    return 100;
	  }
	 return 0;
}



как я понял tablewriter как то хитро перенаправляет stdout для COPY
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / с++ выбор либы
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]