title: C++ MySql get last inserted id
body: <p>I´m having the following code that insert a row into mysql database and return the inserted id:</p>
  
  <pre><code>    /*
       * Execute the statement
       */
      std::string sql = "INSERT INTO TABLE (A, B, C) VALUES (1, 2, 3)";
  
      sql::Statement *stmt;
      stmt = connection-&gt;createStatement();
      stmt-&gt;execute(sql);
  
      /*
       * Get the returned id
       */
      stmt = connection-&gt;createStatement();
      sql::ResultSet *res = stmt-&gt;executeQuery("SELECT @@identity AS id");
      res-&gt;next();
      model.modelId = res-&gt;getInt64("id");
  </code></pre>
  
  <p>My questions are:</p>
  
  <p>Do I really need to call <code>connection-&gt;createStatement()</code> again ? 
  I think this may overload the code, as I need to call twice the database.  </p>
  
  <p>Is there a way to optimize this code ?</p>
  
  <p>Is there other way to get the last inserted id ?</p>
  
  <p>Thanks for helping.</p>