Soci逐行打印出所有数据

时间:2017-02-27 19:13:24

标签: c++ mysql sql soci

在不知道架构的情况下,我该如何做?这就是我到目前为止所做的:

row r;
soci::statement st = (mSql->prepare <<
                "select * from tab",
                soci::into(r));

st.execute();
while (st.fetch())
{
        //want a way to print each row
}

1 个答案:

答案 0 :(得分:0)

我是这样做的,它按列打印出来:

    soci::row v;
    soci::statement st = (mSql->prepare << "SELECT * FROM tab", into(v));
    st.execute(true);  /* with data exchange */
    unsigned int num_fields = v.size();
    std::cout << "fields: " << num_fields << std::endl;
    num_fields = (num_fields <= 9) ? num_fields : 9;
    unsigned long num_rows = (unsigned long)st.get_affected_rows();
    std::cout << "rows: " << num_rows << std::endl;
for (size_t i = 0; i < num_fields; ++i) {
            const soci::column_properties &props = v.get_properties(i);
            std::string s = (std::string)props.get_name();
            int row;
            soci::statement st = (mSql->prepare << "select " << props.get_name().c_str() <<" from map", soci::into(row));
            st.execute();
            while (st.fetch())
            {
                    std::stringstream ss;
                    ss << row;
                    std::string str = ss.str();
                    cout<<str<<" ";
            }
            cout<<endl;
    }