检索 LAST_INSERT_ID() 时 getUInt64 的参数无效
我已在表中添加一条记录,该记录会自动递增主键。我没有运气检索这个新值。 MySQL 文档说在查询中使用SELECT LAST_INSERT_ID();
。我已经这样做了,但无法检索结果。
根据结果集的元数据,数据类型为BIGINT
,列名为LAST_INSERT_ID()
。 C++ 连接器有一个用于结果集的 getUInt64()
,我认为这是正确的使用方法。
ResultSet
类声明包含以下内容:
virtual uint64_t getUInt64(uint32_t columnIndex) const = 0;
virtual uint64_t getUInt64(const std::string& columnLabel) const = 0;
文档没有说明 columnIndex
是从零开始还是从一开始。我尝试了这两种情况,并在这两种情况下都得到了 sql::InvalidArgumentException
。
使用结果集元数据,我检索了列名称并将其直接传递给 getUInt64
方法,但仍然收到 sql::InvalidArgumentException
。这不是一个好的指示(当获取数据时返回的列名不起作用时)。
这是我的代码片段:
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);
}
这是调试输出(来自 wxLogDebug):
10:50:58: Column label: "LAST_INSERT_ID()"
Column name: "LAST_INSERT_ID()"
Column type: "BIGINT"
我的问题: 如何使用 MySQL C++ 连接器检索 LAST_INSERT_ID()?
我需要使用准备好的语句吗?
我在 Windows Vista 和 Windows XP 上使用 MySQL Connector C++ 1.0.5 和 Visual Studio 9 (2008)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已解决。
我在检索数据之前插入了一个
query_results->next()
,并且成功了。Resolved.
I inserted a
query_results->next()
before retrieving the data and that worked.