使用 MySQL C API 和 C++ 获取 MySQL 数据库表中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1194453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:07:47  来源:igfitidea点击:

Fetching rows in a MySQL database table using MySQL C API and C++

c++mysqlcapi

提问by drowneath

I'm confused when trying to fetch table rows in mysql using C++ with MySQL C API.

尝试使用 C++ 和 MySQL C API 在 mysql 中获取表行时,我很困惑。

I can do it easily in PHP, just because C++ is a strongly-typed language so that we also need to take care of the dirty process..

我可以在 PHP 中轻松完成,因为 C++ 是一种强类型语言,因此我们还需要处理肮脏的过程。

This is how I done it in PHP

这就是我在 PHP 中完成的方式

$data = array();
$i = 0;
$query = mysql_query("SELECT * FROM `my_table`");
while($fetch = mysql_fetch_array($query))
{
  $data[$i] = $fetch['columntobefetched'];
  $i++;
}

But how to do the same in C++ with MySQL API?

但是如何在 C++ 中用 MySQL API 做同样的事情呢?

Here's my code so far....with a confusing dead end...x__x

到目前为止,这是我的代码......有一个令人困惑的死胡同......x__x

   MYSQL *sqlhnd = mysql_init(NULL);
    mysql_real_connect(sqlhnd, "server", "user", "pass", "database", port, NULL, 0);

    mysql_query(sqlhnd, "SELECT * FROM `my_table`");
    MYSQL_RES *confres = mysql_store_result(sqlhnd);
    int totalrows = mysql_num_rows(confres);
    int numfields = mysql_num_fields(confres);
    MYSQL_FIELD *mfield;

    while((row = mysql_fetch_row(confres)))
    {
        for(i = 0; i < numfields; i++)
        {
            while(mfield = mysql_fetch_field(confres))
            {
                mfield->//??? I'm dead
            }
        }
    }

Basically I wanted to get a value from a field in the database table and store it to a variable..

基本上我想从数据库表中的一个字段中获取一个值并将其存储到一个变量中。

Any kind of help would be appreciated :)

任何形式的帮助将不胜感激:)

Thanks

谢谢

回答by Avi

In the MySQL C API, mysql_fetch_rowreturns a MYSQL_ROW object, which is essentially an array of values in the current row.

在 MySQL C API 中,mysql_fetch_row返回一个 MYSQL_ROW 对象,它本质上是当前行中的值数组。

So, your code should be something like:

所以,你的代码应该是这样的:

mysql_query(sqlhnd, "SELECT * FROM `my_table`");
MYSQL_RES *confres = mysql_store_result(sqlhnd);
int totalrows = mysql_num_rows(confres);
int numfields = mysql_num_fields(confres);
MYSQL_FIELD *mfield;

while((row = mysql_fetch_row(confres)))
{
    for(i = 0; i < numfields; i++)
    {
        char *val = row[i];
        // do something with val...
    }
}

Better yet, don't do a "SELECT * FROM mytable" in a program. It would be much better to name the fields you expect, so that you can be sure of the order of the fields returned.

更好的是,不要mytable在程序中执行“SELECT * FROM ”。最好为您期望的字段命名,这样您就可以确定返回的字段的顺序。

回答by Arpegius

If you are using C++ why not using MySQL++? This is some sample code:

如果您使用 C++ 为什么不使用MySQL++?这是一些示例代码:

mysqlpp::Connection dbconnection;
dbconnection.connect("database", "server", "user", "pass");

mysqlpp::Query prepared_query = dbconnection.query( "SELECT * FROM `my_table`" );

mysqlpp::StoreQueryResult r = prepared_query.store();

int field = r.field_num("columntobefetched");

for(mysqlpp::StoreQueryResult::iterator i = r.begin(); i!=r.end();i++)
{
      std::cout << i->at(field) << std::endl;
      std::cout << (*i)["columntobefetched"] << std::endl; // this will be slower
}