linux系统下PostgreSQL的使用

打印 上一主题 下一主题

主题 846|帖子 846|积分 2538


前言

最近工作中使用到了pgsql,重要是使用其c++驱动完成数据库创建及增编削查等利用…
一、安装pgsql数据库

使用命令如下:
  1. sudo apt-get install postgresql
复制代码
安装完成,使用如下命令,确认数据库版本:
  1. psql --version
复制代码
二、安装c和c++驱动

使用如下命令安装c驱动:
  1. sudo apt-get install libpq-dev
复制代码
使用如下命令安装c++驱动:
  1. sudo tar -zxvf libpqxx-6.4.8.tar.gz
  2. cd libpqxx-6.4.8/
  3. sudo ./configure --disable-documentation
  4. sudo make
  5. sudo make install
复制代码
三、使用

1、头文件

头文件如下:
  1. #ifndef POSTGREOPERATOR_H
  2. #define POSTGREOPERATOR_H
  3. #include <iostream>
  4. #include <string>
  5. #include <map>
  6. #include <thread>
  7. #include "pqxx/pqxx"
  8. using namespace std;
  9. using namespace pqxx;
  10. struct TableField {
  11.     string name;
  12.     string type;
  13. };
  14. class PostgreOperator
  15. {
  16. private:
  17.     PostgreOperator() {}
  18.     PostgreOperator(const PostgreOperator&) = delete;
  19.     PostgreOperator& operator=(const PostgreOperator&) = delete;
  20.     ~PostgreOperator();
  21. public:
  22.     static PostgreOperator& getInstance();
  23.     bool connect();
  24.     void disConnect();
  25.     bool insertOneRow(const string& tableName,const vector<string>& rowData);
  26.     bool updateOneRow(const string& tableName, const string& conditionColumnName,
  27.                    const string& conditionValue, const vector<string>& columnNames,
  28.                    const vector<string>& newValues);
  29.     void selectRows(const string& tableName, vector<vector<string>>& resultRows,
  30.                     const vector<string>& selectedColumns,
  31.                     const string& conditionColumnName = "",const string& conditionValue = "");
  32.     bool deleteRows(const string& tableName, const string& conditionColumnName = "",
  33.                       const string& conditionValue = "");
  34. private:
  35.     static void initTable();
  36.     static bool createdb(const string& dbname,const string& user,const string& password);
  37.     static bool createInitTable();
  38.     static bool addTable(const string& tableName,const vector<TableField>& fields);
  39.     static bool deleteTable(const string& tableName);
  40.     static bool addFieldToTable(const string& tableName, const vector<TableField>& fields);
  41.     static bool removeFieldFromTable(const string& tableName, const vector<string>& fieldNames);
  42. private:
  43.     static string m_user;
  44.     static string m_passwd;
  45.     static string m_dbName;
  46.     static bool m_initTable;
  47.     static PostgreOperator *m_instance;
  48.     static map<string, vector<TableField>> m_tables;
  49.     static connection* m_pConnection;
  50.     static thread_local unique_ptr<connection> thread_local_connection_;
  51. };
  52. #endif // POSTGREOPERATOR_H
复制代码
2、源文件

源文件如下:
  1. #include "postgreoperator.h"
  2. string      PostgreOperator::m_dbName;
  3. string      PostgreOperator::m_user;
  4. string      PostgreOperator::m_passwd;
  5. bool        PostgreOperator::m_initTable = false;
  6. connection* PostgreOperator::m_pConnection = nullptr;
  7. map<string, vector<TableField>> PostgreOperator::m_tables;
  8. PostgreOperator* PostgreOperator::m_instance = nullptr;
  9. thread_local unique_ptr<connection> PostgreOperator::thread_local_connection_ = nullptr;
  10. PostgreOperator& PostgreOperator::getInstance()
  11. {
  12.     if (!m_instance) {
  13.         m_instance = new PostgreOperator;
  14.         if(!m_initTable){
  15.             initTable();
  16.             createdb("disposaldb","tami","tami");
  17.             createInitTable();
  18.         }
  19.     }
  20.     return *m_instance;
  21. }
  22. PostgreOperator::~PostgreOperator()
  23. {
  24.     if(m_pConnection){
  25.         m_pConnection->disconnect();
  26.         delete m_pConnection;
  27.         m_pConnection = nullptr;
  28.     }
  29. }
  30. bool PostgreOperator::createdb(const string& dbname,const string& user,const string& password)
  31. {
  32.     m_dbName = dbname;
  33.     m_user = user;
  34.     m_passwd = password;
  35.     string connectStr = "dbname=postgres user=postgres password=postgres "
  36.                         "hostaddr=127.0.0.1 port=5432";
  37.     bool ret = false;
  38.     try {
  39.         connection *connection = new pqxx::connection(connectStr);
  40.         if(connection->is_open()){
  41.             nontransaction txn(*connection);
  42.             string quotedDb = "'" + dbname + "'";
  43.             if(user.compare("postgres") == 0 && password.compare("postgres") == 0){
  44.                 //cout<<"hello111 "<<"user ="<<user<<"password ="<<password<<endl;
  45.                 string checkDb = "SELECT 1 FROM pg_database WHERE datname = " + quotedDb;
  46.                 pqxx::result result_check = txn.exec(checkDb);
  47.                 if(result_check.empty()){
  48.                     string sql = "CREATE DATABASE " + dbname + " WITH OWNER= postgres"+" ENCODING='UTF-8' ;";
  49.                     txn.exec(sql);
  50.                     cout << "create database "+ dbname +" with user=postgres successed!" << endl;
  51.                 }
  52.                 else{
  53.                     cout << "database "+ dbname + " already exists!" <<endl;
  54.                 }
  55.             }
  56.             else{
  57.                 //cout<<"hello222 "<<"user ="<<user<<" password ="<<password<<endl;
  58.                 string quotedUser = "'" + user + "'";
  59.                 string checkUser = "SELECT 1 FROM pg_user WHERE usename = " + quotedUser;
  60.                 pqxx::result result_checkUser = txn.exec(checkUser);
  61.                 if (result_checkUser.empty()) {
  62.                     string sql = "CREATE USER " + user + " WITH PASSWORD '" + password + "'";
  63.                     txn.exec(sql);
  64.                 }
  65.                 else{
  66.                     cout << "user "+ user + " already exists!" <<endl;
  67.                 }
  68.                 std::string checkDb = "SELECT 1 FROM pg_database WHERE datname = " + quotedDb;
  69.                 pqxx::result result_check = txn.exec(checkDb);
  70.                 if(result_check.empty()){
  71.                     string dbSql = "CREATE DATABASE " + dbname + " WITH OWNER="+user+" ENCODING='UTF-8';";
  72.                     txn.exec(dbSql);
  73.                     cout << "create database "+ dbname +" with user="+user+" successed!" << endl;
  74.                 }
  75.                 else{
  76.                     cout << "database "+ dbname + " already exists!" <<endl;
  77.                 }
  78.             }
  79.             ret = true;
  80.         }else{
  81.             cout<<"open database " + dbname +" with user=postgres failed!"<<endl;
  82.             connection->disconnect();
  83.             return ret;
  84.         }
  85.         delete connection;
  86.         connection = nullptr;
  87.     }catch (const std::exception &e) {
  88.         cerr<<e.what()<<endl;
  89.     }
  90.     return ret;
  91. }
  92. void PostgreOperator::initTable()
  93. {
  94.     m_tables.clear();
  95.     m_tables = {
  96.         {"p_frequency", {
  97.             {"msgId", "bigint"},
  98.             {"sn", "bigint"},
  99.             {"startFrequency", "bigint"},
  100.             {"endFrequency", "bigint"},
  101.             {"rbw", "double precision"},
  102.             {"dataType", "smallint"},
  103.             {"number", "integer"},
  104.         }},
  105.         {"d_frequency", {
  106.             {"msgId", "bigint"},
  107.             {"sn", "bigint"},
  108.             {"times", "timestamp without time zone"},
  109.             {"timems", "timestamp without time zone"},
  110.             {"data", "smallint[]"},
  111.         }},
  112.         {"p_timedomain", {
  113.              {"msgId", "bigint"},
  114.              {"sn", "bigint"},
  115.              {"centerFrequency", "bigint"},
  116.              {"gain", "smallint"},
  117.              {"timestamp", "timestamp without time zone"},
  118.              {"frameTotal", "integer"},
  119.              {"frameNumber", "integer"},
  120.              {"number", "integer"},
  121.         }},
  122.         {"d_timedomain", {
  123.              {"msgId", "bigint"},
  124.              {"sn", "bigint"},
  125.              {"times", "timestamp without time zone"},
  126.              {"timems", "timestamp without time zone"},
  127.              {"data", "integer[]"},
  128.         }},
  129.     };
  130. }
  131. bool PostgreOperator::createInitTable()
  132. {
  133.     string connectStr = "dbname="+m_dbName+" user="+m_user+" password="+m_passwd+
  134.                         " hostaddr=127.0.0.1 port=5432";
  135.     bool ret = false;
  136.     try {
  137.         m_pConnection = new pqxx::connection(connectStr);
  138.         if(m_pConnection->is_open()){
  139.             nontransaction txn(*m_pConnection);
  140.             for (const auto& table : m_tables) {
  141.                 const std::string tableName = table.first;
  142.                 const std::vector<TableField>& fields = table.second;
  143.                 string quotedtable = "'" + tableName + "'";
  144.                 string checktable = "SELECT 1 FROM information_schema.tables WHERE table_name = " + quotedtable;
  145.                 //cout<<"checktable ="<<checktable<<endl;
  146.                 pqxx::result result_check = txn.exec(checktable);
  147.                 if (result_check.empty()) {
  148.                     string quotedTableName = """ + tableName + """;
  149.                     string createTableQuery = "CREATE TABLE " + quotedTableName + " (";
  150.                     string idStr = "id";
  151.                     string msgIdStr = "msgId";
  152.                     string snStr = "sn";
  153.                     createTableQuery += """ + idStr + "" ";
  154.                     createTableQuery += " BIGSERIAL, ";
  155.                     for (size_t i = 0; i < fields.size(); i++) {
  156.                         if (fields[i].name == msgIdStr || fields[i].name == snStr) {
  157.                             createTableQuery += """ + fields[i].name + "" " + " " + fields[i].type;
  158.                             createTableQuery += " NOT NULL";
  159.                         } else {
  160.                             createTableQuery += """ + fields[i].name + "" " + " " + fields[i].type;
  161.                         }
  162.                         if (i < fields.size() - 1) {
  163.                             createTableQuery += ", ";
  164.                         }
  165.                     }
  166.                     createTableQuery += ", PRIMARY KEY ("" + idStr + "")";
  167.                     createTableQuery += ")";
  168.                     //cout<<"createTableQuery ="<<createTableQuery<<endl;
  169.                     txn.exec(createTableQuery);
  170.                     cout << "create table " + tableName + " succeeded!" << endl;
  171.                 } else {
  172.                     cout << "table " + tableName + " already exists!" << endl;
  173.                 }
  174.             }
  175.             ret = true;
  176.         }
  177.     } catch (const std::exception& e) {
  178.         cerr << e.what() << endl;
  179.     }
  180.     return ret;
  181. }
  182. bool PostgreOperator::connect()
  183. {
  184.     if (!thread_local_connection_) {
  185.         std::string connectStr = "dbname=" + m_dbName + " user=" + m_user + " password=" + m_passwd
  186.                                 + " hostaddr=127.0.0.1 port=5432";
  187.         try {
  188.             thread_local_connection_.reset(new pqxx::connection(connectStr));
  189.             if (thread_local_connection_->is_open()) {
  190.                 cout << "Connected to dbname=" << m_dbName << " with user=" << m_user << " succeeded!" << endl;
  191.                 return true;
  192.             } else {
  193.                 cout << "Failed to connect to dbname=" << m_dbName << " with user=" << m_user << endl;
  194.                 return false;
  195.             }
  196.         } catch (const std::exception& e) {
  197.             std::cerr << "Connection failed: " << e.what() << std::endl;
  198.             return false;
  199.         }
  200.     }
  201.     return true;
  202. }
  203. void PostgreOperator::disConnect()
  204. {
  205.     if (thread_local_connection_) {
  206.         thread_local_connection_->disconnect();
  207.         thread_local_connection_.reset();
  208.     }
  209. }
  210. bool PostgreOperator::addFieldToTable(const string& tableName, const vector<TableField>& fields)
  211. {
  212.     bool ret = false;
  213.     try {
  214.         work txn(*m_pConnection);
  215.         string quotedtable = "'" + tableName + "'";
  216.         string checktable = "SELECT 1 FROM information_schema.tables WHERE table_name = " + quotedtable;
  217.         pqxx::result result_check = txn.exec(checktable);
  218.         if (!result_check.empty()) {
  219.             for(const auto& field : fields){
  220.                 string columnName = "'" + field.name + "'";
  221.                 string sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '" + tableName + "' AND column_name = " +columnName;
  222.                 //cout<<"select sql ="<<sql<<endl;
  223.                 pqxx::result result = txn.exec(sql);
  224.                 if(result.empty()){
  225.                     string sql = "ALTER TABLE "+ tableName+" ADD COLUMN ";
  226.                     sql += """ + field.name + "" " + " " + field.type;
  227.                     //cout<<"alter add sql ="<<sql<<endl;
  228.                     txn.exec(sql);
  229.                     cout << "addFieldToTable successfully." << endl;
  230.                     ret = true;
  231.                 }
  232.                 else{
  233.                     cout<<"column "+field.name+" of "+tableName+" already exists!"<<endl;
  234.                 }
  235.             }
  236.         }
  237.         else{
  238.            cout << "table " + tableName + " does not exist!" << endl;
  239.         }
  240.         txn.commit();
  241.     } catch (const std::exception& e) {
  242.         cerr << e.what() << endl;
  243.     }
  244.     return ret;
  245. }
  246. bool PostgreOperator::removeFieldFromTable(const string& tableName, const vector<string>& fieldNames)
  247. {
  248.     bool ret = false;
  249.     try {
  250.         work txn(*m_pConnection);
  251.         string quotedtable = "'" + tableName + "'";
  252.         string checktable = "SELECT 1 FROM information_schema.tables WHERE table_name = " + quotedtable;
  253.         pqxx::result result_check = txn.exec(checktable);
  254.         if (!result_check.empty()) {
  255.             for(const auto& field : fieldNames){
  256.                 string columnName = "'" + field + "'";
  257.                 string sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '" + tableName + "' AND column_name = " +columnName;
  258.                 //cout<<"select sql ="<<sql<<endl;
  259.                 pqxx::result result = txn.exec(sql);
  260.                 if(!result.empty()){
  261.                     string sql = "ALTER TABLE "+ tableName+" DROP COLUMN ";
  262.                     sql += """ + field + "" " + " ";
  263.                     //cout<<"alter drop sql ="<<sql<<endl;
  264.                     txn.exec(sql);
  265.                     cout << "removeFieldFromTable successfully." << endl;
  266.                     ret = true;
  267.                 }
  268.                 else{
  269.                     cout<<"column "+field+" of "+tableName+" does not exists!"<<endl;
  270.                 }
  271.             }
  272.         }
  273.         else{
  274.            cout << "table " + tableName + " does not exist!" << endl;
  275.         }
  276.         txn.commit();
  277.     } catch (const std::exception& e) {
  278.         cerr << e.what() << endl;
  279.     }
  280.     return ret;
  281. }
  282. bool PostgreOperator::addTable(const string& tableName, const vector<TableField>& fields)
  283. {
  284.     bool ret = false;
  285.     work txn(*m_pConnection);
  286.     string quotedtable = "'" + tableName + "'";
  287.     string checktable = "SELECT 1 FROM information_schema.tables WHERE table_name = " + quotedtable;
  288.     //cout<<"checktable ="<<checktable<<endl;
  289.     pqxx::result result_check = txn.exec(checktable);
  290.     if (result_check.empty()) {
  291.         string quotedTableName = """ + tableName + """;
  292.         string createTableQuery = "CREATE TABLE " + quotedTableName + " (";
  293.         string idStr = "id";
  294.         string msgIdStr = "msgId";
  295.         string snStr = "sn";
  296.         createTableQuery += """ + idStr + "" ";
  297.         createTableQuery += " BIGSERIAL, ";
  298.         createTableQuery += """ + msgIdStr + "" ";
  299.         createTableQuery += " bigint NOT NULL, ";
  300.         createTableQuery += """ + snStr + "" ";
  301.         createTableQuery += " bigint NOT NULL, ";
  302.         if(!fields.empty()){
  303.             for (size_t i = 0; i < fields.size(); i++) {
  304.                 createTableQuery += """ + fields[i].name + "" " + " " + fields[i].type;
  305.                 if (i < fields.size() - 1) {
  306.                     createTableQuery += ", ";
  307.                 }
  308.             }
  309.             createTableQuery += ", PRIMARY KEY ("" + idStr + "")";
  310.         }
  311.         else{
  312.             createTableQuery += " PRIMARY KEY ("" + idStr + "")";
  313.         }
  314.         createTableQuery += ")";
  315.         txn.exec(createTableQuery);
  316.         txn.commit();
  317.         //cout<<"createTableQuery ="<<createTableQuery<<endl;
  318.         cout << "create table " + tableName + " succeeded!" << endl;
  319.         m_tables[tableName] = fields;
  320. //        for (const auto& table : m_tables) {
  321. //            const string tableName = table.first;
  322. //            cout<<"tablename ="<<tableName<<endl;
  323. //            const vector<TableField>& fields = table.second;
  324. //            for(const auto& field : fields){
  325. //                cout<<"name="<<field.name<<"type ="<<field.type;
  326. //            }
  327. //            cout<<endl;
  328. //        }
  329.         ret = true;
  330.     } else {
  331.         cout << "table " + tableName + " already exists!" << endl;
  332.     }
  333.     return ret;
  334. }
  335. bool PostgreOperator::deleteTable(const string& tableName)
  336. {
  337.     bool ret = false;
  338.     try {
  339.         string sql = "DROP TABLE IF EXISTS " + tableName;
  340.         pqxx::work txn(*m_pConnection);
  341.         txn.exec(sql);
  342.         if(!m_tables.empty()){
  343.             auto it = m_tables.find(tableName);
  344.             if (it != m_tables.end()) {
  345.                 m_tables.erase(it);
  346.                 //cout << "Table '" << tableName << "' deleted from m_tables successfully." << endl;
  347.                 ret = true;
  348.             } else {
  349.                 cout << "Table '" << tableName << "' not found from m_tables." << endl;
  350.             }
  351.         }
  352.         txn.commit();
  353.     } catch (const std::exception& e) {
  354.         cerr << e.what() << endl;
  355.     }
  356.     return ret;
  357. }
  358. bool PostgreOperator::insertOneRow(const string& tableName,const vector<string>& rowData)
  359. {
  360.     bool ret = false;
  361.     pqxx::work txn(*thread_local_connection_);
  362.     try {
  363.         std::string sql = "INSERT INTO " + tableName + " (";
  364.         for (const auto& table : m_tables) {
  365.             const std::string tableNameStr = table.first;
  366.             const std::vector<TableField>& fields = table.second;
  367.             if(tableNameStr == tableName){
  368.                 for (size_t i = 0; i < fields.size(); i++) {
  369.                     sql += """ + fields[i].name + "" ";
  370.                     if (i != fields.size() - 1) {
  371.                         sql += ",";
  372.                     }
  373.                 }
  374.             }
  375.         }
  376.         sql += ") VALUES (";
  377.         for (size_t i = 0; i < rowData.size(); i++) {
  378.             sql += "'" + rowData[i] + "'";
  379.             if (i != rowData.size() - 1) {
  380.                 sql += ",";
  381.             }
  382.         }
  383.         sql += ")";
  384.         //cout<<"insert sql ="<<sql<<endl;
  385.         txn.exec(sql);
  386.         txn.commit();
  387.         ret = true;
  388.     } catch (const std::exception &e) {
  389.         cerr<<e.what()<<endl;
  390.         txn.abort();
  391.     }
  392.     return ret;
  393. }
  394. bool PostgreOperator::updateOneRow(const string& tableName, const string& conditionColumnName,
  395.                const string& conditionValue, const vector<string>& columnNames,
  396.                const vector<string>& newValues)
  397. {
  398.     bool ret = false;
  399.     try {
  400.         pqxx::work txn(*thread_local_connection_);
  401.         std::string sql = "UPDATE " + tableName + " SET ";
  402.         for (size_t i = 0; i < columnNames.size(); i++) {
  403.             string columnName = """ + columnNames[i] + "" ";
  404.             sql += columnName + " = '" + newValues[i] + "'";
  405.             if (i != columnNames.size() - 1) {
  406.                 sql += ",";
  407.             }
  408.         }
  409.         string conditionName = """ + conditionColumnName + "" ";
  410.         sql += " WHERE " + conditionName + " = '" + conditionValue + "'";
  411.         txn.exec(sql);
  412.         txn.commit();
  413.         cout << "Data updated successfully." << endl;
  414.         ret = true;
  415.     } catch (const std::exception &e) {
  416.         cerr << e.what() << endl;
  417.     }
  418.     return ret;
  419. }
  420. void PostgreOperator::selectRows(const string& tableName, vector<vector<string>>& resultRows, const vector<string>& selectedColumns,
  421.                                  const string& conditionColumnName,const string& conditionValue)
  422. {
  423.     try {
  424.         pqxx::work txn(*thread_local_connection_);
  425.         string sql;// = "SELECT * FROM " + tableName;
  426.         if (!selectedColumns.empty()) {
  427.             sql = "SELECT ";
  428.             for (size_t i = 0; i < selectedColumns.size(); ++i) {
  429.                 sql += """ + selectedColumns[i] + """;
  430.                 if (i < selectedColumns.size() - 1)
  431.                     sql += ",";
  432.             }
  433.             sql += " FROM " + tableName;
  434.         } else {
  435.             sql = "SELECT * FROM " + tableName;
  436.         }
  437.         if(!conditionColumnName.empty() && !conditionValue.empty()) {
  438.             string conditionName = """ + conditionColumnName + "" ";
  439.             sql += " WHERE " + conditionName + " = '" + conditionValue + "'";
  440.         }
  441.         pqxx::result result = txn.exec(sql);
  442.         for (const auto& row : result) {
  443.             vector<string> record;
  444.             for (const auto& field : row) {
  445.                 //cout << field.name() << ": " << field.c_str() << "     ";
  446.                 record.push_back(field.c_str());
  447.             }
  448.             resultRows.push_back(record);
  449.             //cout << endl;
  450.         }
  451.         txn.commit();
  452.         cout << "Data select successfully." << endl;
  453.     } catch (const std::exception &e) {
  454.         cerr << e.what() << endl;
  455.     }
  456. }
  457. bool PostgreOperator::deleteRows(const string& tableName, const string& conditionColumnName,
  458.                                    const string& conditionValue)
  459. {
  460.     bool ret = false;
  461.     pqxx::work txn(*thread_local_connection_);
  462.     try {
  463.         string sql = "DELETE FROM " + tableName;
  464.         if(!conditionColumnName.empty() && !conditionValue.empty()) {
  465.             string conditionName = """ + conditionColumnName + "" ";
  466.             sql += " WHERE " + conditionName + " = '" + conditionValue + "'";
  467.         }
  468.         cout<<"deleterows sql = "<<sql<<endl;
  469.         txn.exec(sql);
  470.         txn.commit();
  471.         cout << "Data delete successfully." << endl;
  472.         ret = true;
  473.     } catch (const std::exception &e) {
  474.         cerr << e.what() << endl;
  475.         txn.abort();
  476.     }
  477.     return ret;
  478. }
复制代码
3、main文件

  1. #include <chrono>
  2. #include <iostream>
  3. #include <thread>
  4. #include "postgreoperator.h"
  5. void insertfun(int id,PostgreOperator &operatorInstance) {
  6.     if (!operatorInstance.connect()) {
  7.         std::cerr << "Thread " << id << " failed to connect to the database." << std::endl;
  8.         return;
  9.     }
  10.     string tableName = "p_frequency";
  11.     for(int i = 0;i<500;i++){
  12.         vector<string> data = {"12345", "10086", "0", "2000000", "0.1", "1", "10"};
  13.         operatorInstance.insertOneRow(tableName, data);
  14.     }
  15.     for(int i = 500;i<1000;i++){
  16.         vector<string> data = {"67890", "10086", "0", "2000000", "0.1", "1", "10"};
  17.         operatorInstance.insertOneRow(tableName, data);
  18.     }
  19. }
  20. void deletefun(int id,PostgreOperator &operatorInstance)
  21. {
  22.     if (!operatorInstance.connect()) {
  23.         std::cerr << "Thread " << id << " failed to connect to the database." << std::endl;
  24.         return;
  25.     }
  26.     string tableName = "p_frequency";
  27.     string conditionColumnName = "msgId";
  28.     string conditionValue = "12345";
  29.     operatorInstance.deleteRows(tableName, conditionColumnName, conditionValue);
  30. }
  31. int main(int argc, char *argv[])
  32. {
  33.     PostgreOperator& operatorInstance = PostgreOperator::getInstance();
  34.     // 创建并启动多个线程
  35.     std::vector<std::thread> threads;
  36.     for (int i = 0; i < 5; ++i) {
  37.         threads.emplace_back(insertfun, i,std::ref(operatorInstance));
  38.     }
  39.     // 等待所有线程完成
  40.     for (auto& thread : threads) {
  41.         thread.join();
  42.     }
  43. #if 0
  44.     std::thread t1(deletefun, 5, std::ref(operatorInstance));
  45.     t1.join();
  46. #endif
  47.     return 1;
  48. }
复制代码
4、编译

命令如下:
  1. g++ -pthread --std=c++11 -o demo.out main.cpp postgreoperator.cpp -lpqxx -lpq
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

渣渣兔

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表