我在尝试使用 C++ 和 MySQL C API 在 mysql 中获取表行时感到困惑.
I'm confused when trying to fetch table rows in mysql using C++ with MySQL C API.
我可以在 PHP 中轻松完成,只是因为 C++ 是一种强类型语言,因此我们还需要处理肮脏的过程..
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 中的做法
This is how I done it in PHP
$data = array();
$i = 0;
$query = mysql_query("SELECT * FROM `my_table`");
while($fetch = mysql_fetch_array($query))
{
$data[$i] = $fetch['columntobefetched'];
$i++;
}
但是如何在 C++ 中用 MySQL API 做同样的事情?
But how to do the same in C++ with MySQL API?
到目前为止,这是我的代码......有一个令人困惑的死胡同......x__x
Here's my code so far....with a confusing dead end...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 :)
谢谢
在 MySQL C API 中,mysql_fetch_row 返回一个 MYSQL_ROW 对象,它本质上是当前行中的值数组.
In the MySQL C API, mysql_fetch_row returns a MYSQL_ROW object, which is essentially an array of values in the current 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...
}
}
最好不要在程序中执行SELECT * FROM mytable".最好为您期望的字段命名,这样您就可以确定返回的字段的顺序.
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.
这篇关于使用 MySQL C API 和 C++ 获取 MySQL 数据库表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何有效地使用窗口函数根据 N 个先前值来决定How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函数根据
在“GROUP BY"中重用选择表达式的结果;条款reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用选择表达式的结果;条款?)
Pyspark DataFrameWriter jdbc 函数的 ignore 选项是忽略整Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函数的 ig
使用 INSERT INTO table ON DUPLICATE KEY 时出错,使用 Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 时出错,使用 for 循环数组
pyspark mysql jdbc load 调用 o23.load 时发生错误 没有合pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 调用 o23.load 时发生错误 没有合适的
如何将 Apache Spark 与 MySQL 集成以将数据库表作为How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何将 Apache Spark 与 MySQL 集成以将数据库表作为