简体   繁体   English

行的SOCI向量

[英]SOCI Vector of row

I'm trying to get a vector of row from SOCI, but I can't seem to work it out. 我正在尝试从SOCI获取行向量,但似乎无法解决。

I need a function to return a vector of row data from a sql statement. 我需要一个函数来从sql语句返回行数据的向量。 What I think is the right approach: 我认为正确的方法是:

vector<row> GetRows(string input)
{
    session sql(*soci::factory_odbc(), _connectionString);

    row r;
    statement st = (sql.prepare << input, into(r));
    st.execute();
    vector<row> rows;
    while (st.fetch())
    {
        rows.push_back(r);
    }
    return(rows);
}

This will not compile. 这将无法编译。

I have to index to the rows, I can't see how to this to work. 我必须索引到行,我看不到它如何工作。

Update: 更新:

I don't have that much expirence with C++ so I ended up using a simple solution: 我对C ++的期望不高,所以我最终使用了一个简单的解决方案:

typedef boost::variant<boost::blank, int, std::string, double, std::tm, long long, unsigned long long> tField;
typedef std::vector<tField> tRow;
typedef std::vector<tRow> tRows;

Where tField is a boost::variant and the row (tRow) is a vector of those. 其中tField是boost :: variant,而行(tRow)是这些的向量。 The dataset (tRows) is then a vector of these. 然后,数据集(tRows)是这些的向量。

I ended up with this, not so simple but working solition: 我最终得到了这个,不是那么简单,而是工作的孤独:

tRows DataAccess::spGetListenSample(const string input)
{
    session sql(*soci::factory_odbc(), _connectionString);

    rowset<row> rs = (sql.prepare << input);

    tRows rows;

    for (rowset<row>::const_iterator it = rs.begin(); it != rs.end(); ++it)
    {
        row const& r = *it;
        if (rows.size() == 0)
        {
            tRow record;
            for (std::size_t i = 0; i != r.size(); ++i)
            {
                const column_properties & props = r.get_properties(i);
                tField field = props.get_name();
                record.push_back(field);
            }
            rows.push_back(record);
        }
        tRow record;
        for (std::size_t i = 0; i != r.size(); ++i)
        {
            const column_properties & props = r.get_properties(i);
            tField field;
            if (r.get_indicator(i) != soci::i_null)
            {
                switch (props.get_data_type())
                {
                case dt_string:
                    field = r.get<std::string>(i);
                    break;
                case dt_double:
                    field = r.get<double>(i);
                    break;
                case dt_integer:
                    field = r.get<int>(i);
                    break;
                case dt_long_long:
                    field = r.get<long long>(i);
                    break;
                case dt_unsigned_long_long:
                    field = r.get<unsigned long long>(i);
                    break;
                case dt_date:
                    std::tm when = r.get<std::tm>(i);
                    field = asctime(&when);
                    break;
                }
            }
            record.push_back(field);
        }
        rows.push_back(record);
    }

    return(rows);
}

你不能push_back类型的值soci::rowstd::vector自级soci::rowSOCI_NOT_COPYABLE(row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM