帮助我,我想创建一个泛型函数来获取任何查询字符串的结果,并将所有行放入另一个变量中。迭代一行时,如何知道可用的列数?
Environment *env = Environment::createEnvironment();
Connection *conn = env->createConnection("user","pass");
quantLinhas = 0;
if( conn != NULL ) {
Statement *stmt = conn->createStatement(query);
ResultSet *rs = stmt->executeQuery();
resultadoSQL->linhas.clear();
while (rs->next()) {
aux.campos.clear();
numbers_colums = rs->whatever_method() //WHAT DO I DO HERE??
for(i = 0; i < numbers_colums ; i++) {
aux.campos.push_back( rs->getString(i) );
quantLinhas++;
}
resultadoSQL->linhas.push_back( aux );
}
stmt->closeResultSet(rs);
conn->terminateStatement(stmt);
env->terminateConnection(conn);
}
答案 0 :(得分:1)
根据documentation,您可以使用getColumnListMetaData
功能。返回的向量中的元素数可能是您想要的。您可以检查每个元素是否为PTYPE_COL以确保它。
答案 1 :(得分:0)
尝试
vector<MetaData> columnList = rs->getColumnListMetaData();
numbers_colums = columnList.size();
columnList.clear();
它为我工作。 source link