Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 783761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:36:03+00:00 2026-05-14T20:36:03+00:00

I have added a record to my table which auto-increments the primary key. I

  • 0

I have added a record to my table which auto-increments the primary key. I am having no luck retrieving this new value. The MySQL documents say to use the SELECT LAST_INSERT_ID(); in a query. I have done this, but can’t retrieve the results.

According the the metadata of the result set, the data type is BIGINT and the column name is LAST_INSERT_ID(). The C++ connector has a getUInt64() for the result set, which I assume is the correct method to use.

The ResultSet class declaration contains the following:

virtual uint64_t getUInt64(uint32_t columnIndex) const = 0;
virtual uint64_t getUInt64(const std::string& columnLabel) const = 0;

The documentation does not state whether the columnIndex is zero based or one based. I tried both and get sql::InvalidArgumentException for both cases.

Using the result set metadata, I retrieved the column name and passed it directly to the getUInt64 method and still receive the sql::InvalidArgumentException. This not a good indication (when the returned column name doesn’t work when fetching the data).

Here is my code fragment:

std::string query_text;
query_text = "SELECT LAST_INSERT_ID();";
boost::shared_ptr<sql::Statement>   query(m_db_connection->createStatement());
boost::shared_ptr<sql::ResultSet>   query_results(query->executeQuery(query_text));
long    id_value = 0;
if (query_results)
{
    ResultSetMetaData p_metadata = NULL;
    p_metadata = query_results->getMetaData();
    unsigned int columns = 0;
    columns = p_metadata->getColumnCount();
    std::string column_label;
    std::string column_name;
    std::string column_type;
    for (i = 0; i < columns; ++i)
    {
        column_label = p_metadata->getColumnLabel(i);
        column_name  = p_metadata->getColumnName(i);
        column_type  = p_metadata->getColumnTypeName(i);
        wxLogDebug("Column label: \"%s\"\nColumn name: \"%s\"\nColumn type: \"%s\"\n",
                   column_label.c_str(),
                   column_name.c_str(),
                   column_type.c_str());
    }
    unsigned int    column_index = 0;
    column_index = query_results->findColumn(column_name);
    // The value of column_index is 1 (one).

    // All of the following will generate sql::InvalidArgumentException
    id_value = query_results->getUInt64(column_index);
    id_value = query_results->getUInt64(column_name);
    id_value = query_results->getUInt64(0);
    id_value = query_results->getUInt64(1);
    id_record.set_record_id(id_value);
}

Here is the debug output (from wxLogDebug):

10:50:58: Column label: "LAST_INSERT_ID()"
Column name: "LAST_INSERT_ID()"
Column type: "BIGINT"

My Question: How do I retrieve the LAST_INSERT_ID() using the MySQL C++ Connector?

Do I need to use a prepared statement instead?

I am using MySQL Connector C++ 1.0.5 on Windows Vista and Windows XP with Visual Studio 9 (2008).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:36:04+00:00Added an answer on May 14, 2026 at 8:36 pm

    Resolved.

    I inserted a query_results->next() before retrieving the data and that worked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have added some code which compiles cleanly and have just received this Windows
I have a table (SQL 2000) with over 10,000,000 records. Records get added at
EDIT: I have added Slug column to address performance issues on specific record selection.
I have an existing database with the table Transactions in it. I have added
I have added the required assemblies and registered the NVelocityViewFactory in global.asax.cs page but
If I have added/removed/modified a large number of files in my local ClearCase view,
I have a gridview that I have added a checkbox column using a templatefield
I have a program with two TForm classes and have added a TMainMenu to
I have written a control in C# that derives from System.Windows.Forms.Control. I have added
How do I access an image during run time that I have added to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.