title: libpqxx v12 - dynamically bind vector-values to prepared statement (alternative to invocation)
body: <p>I would like to use prepared statements to insert thousands of rows at once into my postgres database. The data to insert is stored in a vector of structs.</p>
  <p>By reading the answers to <a href="https://stackoverflow.com/questions/31833322/how-to-prepare-statements-and-bind-parameters-in-postgresql-for-c">How to prepare statements and bind parameters in Postgresql for C++</a> I thought I found the way how to do it.</p>
  <p>Unfortunately, the current version of libpqxx I am using doesn't support <strong>pqxx::prepare::invocation</strong> anymore and I wasn't able to find any alternative in the docs/internet.</p>
  <p>The code I tried out for my purpose is the following:</p>
  <pre><code>//pqxx header
  #include &quot;pqxx/connection.hxx&quot; 
  #include &quot;pqxx/transaction.hxx&quot;
  #include &quot;pqxx/nontransaction.hxx&quot;
  #include &quot;pqxx/compiler-public.hxx&quot;
  #include &quot;pqxx/result.hxx&quot;
  #include &quot;pqxx/prepared_statement.hxx&quot;
  #include &quot;pqxx/transaction_base.hxx&quot;
  #include &quot;pqxx/internal/statement_parameters.hxx&quot;
  
  ...
  int main(){
  
      struct testData {
         string uuid;
         float rndNo;
         string timestamp;
      };
  
      vector&lt;testData&gt; dataBuffer;
      testData dbdata;
  
      string queryStr;
      const char* query;
  
      stringstream connStream;
      stringstream queryStream;
  
      string DB_NAME = &quot;dbname&quot;;
      string DB_USER = &quot;postgres&quot;;
      string DB_PASSWORD = &quot;password&quot;;
      string DB_HOSTADDR = &quot;127.0.0.1&quot;;
      string DB_PORT = &quot;1234&quot;;
      string DB_SCHEMA = &quot;test_schema&quot;;
      string tableName = &quot;test_table&quot;;
  
      //Create Random data 
  
      for (int i = 0; i &lt; 100000; i++) {
  
          dbdata.uuid = &quot;fe2b22ba-6ce7-4bbc-8226-d7696fe7a047&quot;;
          dbdata.rndNo = i / 3.123;
          dbdata.timestamp = systemDateTimeSQLFormat(0);
  
          dataBuffer.push_back(dbdata);
      }
  
      stringstream connStream;
      connStream &lt;&lt; &quot;dbname = &quot; &lt;&lt; DB_NAME &lt;&lt; &quot; user = &quot; &lt;&lt; DB_USER &lt;&lt; &quot; password = &quot; &lt;&lt; DB_PASSWORD &lt;&lt; &quot; hostaddr = &quot; &lt;&lt; DB_HOSTADDR &lt;&lt; &quot; port = &quot; &lt;&lt; DB_PORT;
  
      connection dbConn(connStream.str());
      if (dbConn.is_open()) {
          cout &lt;&lt; &quot;Opened database successfully: &quot; &lt;&lt; dbConn.dbname() &lt;&lt; endl;
      }
      else {
          cout &lt;&lt; &quot;Can't open database&quot; &lt;&lt; endl;
          return;
      }
  
  
      pqxx::nontransaction W(dbConn);
      std::string m_insertCommand = &quot;INSERT INTO test_schema.test_table (column1, column2, column3) VALUES&quot;;
  
      int buffSize = dataBuffer.size();
  
      for (size_t i = 0; i &lt; buffSize; i++)
      {
          unsigned int countOf$ = i * 3;
          for (unsigned int i = 0; i &lt; 3; ++i)
          {
              if (i == 0)
              {
                  m_insertCommand += &quot;(&quot;;
              }
              else
              {
                  m_insertCommand += &quot;, &quot;;
              }
              m_insertCommand += &quot;$&quot;;
              std::stringstream ss;
              ss &lt;&lt; countOf$ + i + 1;
              m_insertCommand += ss.str();
          }
          if (i &lt; buffSize - 1)
              m_insertCommand += &quot;) ,&quot;;
      }
      m_insertCommand += &quot;)&quot;;
  
  
      //FOLLOWING CODE NOT POSSIBLE WITH libpqxx v12
  
      dbConn.prepare(&quot;insert_into_db&quot;, m_insertCommand);
      pqxx::prepare::invocation = W.exec_prepared(&quot;insert_into_db&quot;);  //prepare doesn't have a member &quot;invocation&quot;
  
  
      for (size_t i = 0; i &lt; buffSize; i++)
      {
          inv(dataBuffer.at(i).rndNo)(dataBuffer.at(i).timestamp)(dataBuffer.at(i).uuid); //inv not defined
      }
  
      inv.exec();
  
  return 1;
  }
  </code></pre>