Search code examples
c++mysqldatabasewrappersoci

How can I get the schema or row names of a table in soci?


I am trying to accomplish this, and I know how to do it indirectly...if I can get the schema of a table.

How can I do this using soci?

I have tried:

std::string i;
soci::statement st = (mSql->prepare <<
               "show create table tab;",
               soci::into(i));
st.execute();
while (st.fetch())
{   
         std::cout << i <<'\n';
}

but only "tab" gets printed.

I also tried this, from the Soci documentation in GitHub:

soci::column_info ci;
soci::statement st = (mSql->prepare_column_descriptions(table_name), into(ci));

st.execute();
while (st.fetch())
{
    // ci fields describe each column in turn
}

but was told that column_info is not a member of soci.


Solution

  • I found the following code here

    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::cout << props.get_name() << '\t';
    }
    std::cout << std::endl;
    

    The last things printed are the correct names of the columns.