代码之家  ›  专栏  ›  技术社区  ›  Thomas Matthews

检索最后一个\u INSERT \u ID()时getUInt64的参数无效

  •  0
  • Thomas Matthews  · 技术社区  · 16 年前

    我在表中添加了一条自动递增主键的记录。我无法检索此新值。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 方法并仍然接收 . 这不是一个好的指示(当获取数据时返回的列名不起作用时)。

    这是我的代码片段:

    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);
    }
    

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

    我需要用事先准备好的陈述来代替吗?

    1 回复  |  直到 16 年前
        1
  •  0
  •   Thomas Matthews    16 年前

    断然的。

    我插入了一个 query_results->next()