Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用MysqlPool::executeSql(const char* sql) 函数得到的信息字段名和 单元格内容匹配不上 #4

Open
cuijiecheng opened this issue Jan 29, 2023 · 0 comments

Comments

@cuijiecheng
Copy link

原因是因为std::map是会自动排序的。所以建议将MysqlPool::executeSql(const char* sql)函数改成:

struct TableInfo{
public:
std::string strFieldName;
std::vector<const char*> vecColumnInfo;
};

/*

  • sql语句执行函数,并返回结果,没有结果的SQL语句返回空结果,

  • 每次执行SQL语句都会先去连接队列中去一个连接对象,

  • 执行完SQL语句,就把连接对象放回连接池队列中。

  • 返回对象用map主要考虑,用户可以通过数据库字段,直接获得查询的字。

  • 例如:m["字段"][index]。
    /
    std::map<const std::string,std::vector<const char
    > > MysqlPool::executeSql(const char* sql) {
    MYSQL* conn = getOneConnect();
    std::map<const std::string,std::vector<const char*> > results;
    std::vector vecResults;
    //std::vectorstd::string vecFieldName;
    if (conn) {
    if (mysql_query(conn,sql) == 0) {
    MYSQL_RES *res = mysql_store_result(conn);
    if (res) {
    MYSQL_FIELD field;
    while ((field = mysql_fetch_field(res))) {
    //results.insert(make_pair(field->name,std::vector<const char
    >()));
    TableInfo tableInfo;
    tableInfo.strFieldName = field->name;
    vecResults.push_back(tableInfo);
    }

     MYSQL_ROW row;
     while ((row = mysql_fetch_row(res))) {
       unsigned int i = 0;
    

/*
for (std::map<const std::string,std::vector<const char*> >::iterator it = results.begin();
it != results.end(); ++it) {
(it->second).push_back(row[i++]);
}
*/
for(auto &&e:vecResults)
{
e.vecColumnInfo.push_back(row[i++]);
}
for(auto &&e:vecResults)
{
results.insert(make_pair(e.strFieldName, e.vecColumnInfo));
}
}
mysql_free_result(res);
} else {
if (mysql_field_count(conn) != 0)
std::cerr << mysql_error(conn) << std::endl;
}
} else {
std::cerr << mysql_error(conn) <<std::endl;
}
close(conn);
} else {
std::cerr << mysql_error(conn) << std::endl;
}
return results;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant