如何在soci中获取表的模式或行名?

时间:2017-03-03 17:32:38

标签: c++ mysql database wrapper soci

我正在努力完成this,我知道如何间接地做...如果我可以获得表格的模式。

我怎样才能使用社交网站?

我试过了:

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

但只有&#34; tab&#34;得到印刷。

我也试过这个,来自GitHub中的Soci文档:

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
}

但被告知column_info不是社会成员。

1 个答案:

答案 0 :(得分:2)

我找到了以下代码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;

打印的最后一些内容是列的正确名称。