C++ 检索最后一个\u INSERT\u ID()时getUInt64的参数无效
我在表中添加了一条自动递增主键的记录。我无法检索此新值。MySQL文档说使用C++ 检索最后一个\u INSERT\u ID()时getUInt64的参数无效,c++,mysql,database,mysql-connector,lastinsertid,C++,Mysql,Database,Mysql Connector,Lastinsertid,我在表中添加了一条自动递增主键的记录。我无法检索此新值。MySQL文档说使用SELECT LAST_INSERT_ID()在查询中。我已经这样做了,但无法检索结果 根据结果集的元数据,数据类型为BIGINT,列名为LAST\u INSERT\u ID()。C++连接器的结果集有 GETUIN 64()/,我认为这是正确的使用方法。 ResultSet类声明包含以下内容: virtual uint64_t getUInt64(uint32_t columnIndex) const = 0; vir
SELECT LAST_INSERT_ID()代码>在查询中。我已经这样做了,但无法检索结果
根据结果集的元数据,数据类型为BIGINT
,列名为LAST\u INSERT\u ID()
。C++连接器的结果集有<代码> GETUIN 64()/<代码>,我认为这是正确的使用方法。
ResultSet
类声明包含以下内容:
virtual uint64_t getUInt64(uint32_t columnIndex) const = 0;
virtual uint64_t getUInt64(const std::string& columnLabel) const = 0;
文档没有说明列索引是基于零还是基于一。我尝试了这两种方法,得到了这两种情况下的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);
}
我的问题:<强>我如何使用MySQL C++连接器检索LastIsIdTyIdId()?< /强>
我是否需要使用事先准备好的声明
我使用Visual Studio 9(2008)在WindowsVista和WindowsXP上使用MySQL连接器C++ 1.0.5。< /P> < P>解析。
在检索数据之前,我插入了一个查询结果->下一步()
10:50:58: Column label: "LAST_INSERT_ID()"
Column name: "LAST_INSERT_ID()"
Column type: "BIGINT"