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

  • SEARCH
  • Home
  • 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 just added a null password field to a mysql table using this
I have a query where a record gets added to the table channels. Each
I have an invoices table which stores a single record for each invoice, with
I have added an extra field Specification in Catalog->Product->Data tab. This text is stored
Possible Duplicate: Datetime vs Timestamp? I have a Mysql table, which has a column
Situation: In my database I have a table in which records may reference other
I have parent and child entries in same table, For differentiate i have added
I have a table in database called user which has following entries. Userid FirstName
I have a table which holds the products in a shopping basket (stowaway_orders) and
Good Morning, I have 2 views: ICCUDays which contains one record per account with

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.