MYSQL C APIには、リクエストによって返された行数を返す関数がありますmysql_num_rows
(http://dev.mysql.com/doc/refman/5.0/en/mysql-num-rows.html)。
リクエストで使用できます:
SELECT * FROM MY_TABLE;
そして、テーブルの行数を取得しました。
ここにサンプルコードがあります:
MYSQL * myh
MYSQL_RES *query_result;
unsigned long table_num_rows;
/* Select all records present in the table */
if (0 != mysql_query(myh, "SELECT * FROM MY_TABLE"))
{
fprintf(stderr, "FAIL to perform the query : 'SELECT * FROM MY_TABLE' %s\n", mysql_error(myh));
exit (EXIT_FAILURE);
}
query_result = mysql_store_result(myh);
if (query_result)
{
/* Retreive the number of rows returned by the query, which is the total number of rows in the table
* in our case.
*/
table_num_rows = mysql_num_rows(query_result);
fprintf(stdout, "Our table contain %lu\n", table_num_rows)
}
MYSQL C APIの使用方法の完全な例については、ここで答えを読むことができます
。cを使用してシングルボードコンピューターからmysqlデータベースに書き込む
編集:この関数は、任意のテーブル( mysqldump.cdump_table
)をダンプするときに使用されるようです。行数をカウントするコードを追加するのに適した場所です。このように変更します(いくつかのテストを行う必要があります。私のマシンでコードをテストしていません!):
static void dump_table(char *table, char *db)
{
char ignore_flag;
char buf[200], table_buff[NAME_LEN+3];
DYNAMIC_STRING query_string;
char table_type[NAME_LEN];
char *result_table, table_buff2[NAME_LEN*2+3], *opt_quoted_table;
int error= 0;
ulong rownr, row_break, total_length, init_length;
uint num_fields;
MYSQL_RES *res;
MYSQL_RES *res_num_row; /* Add this */
MYSQL_FIELD *field;
MYSQL_ROW row;
char select_expr[QUERY_LENGTH];
unsigned long table_num_rows; /* Add this */
char table_num_rows_query[256]; /* Add this */
DBUG_ENTER("dump_table");
/* Add this */
/* Build the query to get the number of rows */
snprintf(table_num_rows_query, 256, "SELECT * FROM %s", table);
/*
* Make sure you get the create table info before the following check for
* --no-data flag below. Otherwise, the create table info won't be printed.
* */
num_fields= get_table_structure(table, db, table_type, &ignore_flag);
/*
* The "table" could be a view. If so, we don't do anything here.
* */
if (strcmp(table_type, "VIEW") == 0)
DBUG_VOID_RETURN;
/* Check --no-data flag */
if (opt_no_data)
{
verbose_msg("-- Skipping dump data for table '%s', --no-data was used\n",
table);
DBUG_VOID_RETURN;
}
DBUG_PRINT("info",
("ignore_flag: %x num_fields: %d", (int) ignore_flag,
num_fields));
/*
* If the table type is a merge table or any type that has to be
* _completely_ ignored and no data dumped
* */
if (ignore_flag & IGNORE_DATA)
{
verbose_msg("-- Warning: Skipping data for table '%s' because " \
"it's of type %s\n", table, table_type);
DBUG_VOID_RETURN;
}
/* Check that there are any fields in the table */
if (num_fields == 0)
{
verbose_msg("-- Skipping dump data for table '%s', it has no fields\n",
table);
DBUG_VOID_RETURN;
}
/*
* Check --skip-events flag: it is not enough to skip creation of events
* discarding SHOW CREATE EVENT statements generation. The myslq.event
* table data should be skipped too.
*/
if (!opt_events && !my_strcasecmp(&my_charset_latin1, db, "mysql") &&
!my_strcasecmp(&my_charset_latin1, table, "event"))
{
verbose_msg("-- Skipping data table mysql.event, --skip-events was used\n");
DBUG_VOID_RETURN;
}
result_table= quote_name(table,table_buff, 1);
opt_quoted_table= quote_name(table, table_buff2, 0);
if (opt_lossless_fp && get_select_expr(table, select_expr))
exit(EX_MYSQLERR);
verbose_msg("-- Sending SELECT query...\n");
/* Add this */
/* TODO : check if this is the right place to put our request */
if (0 != mysql_query(mysql, table_num_rows_query))
{
fprintf(stderr, "FAIL to perform the query : %s - %s\n", table_num_rows_query, mysql_error(myh));
exit (EXIT_FAILURE);
}
res_num_row = mysql_store_result(mysql);
if (res_num_row)
{
/* Retreive the number of rows returned by the query, which is the total number of rows in the table
* in our case.
*/
table_num_rows = mysql_num_rows(res_num_row);
fprintf(stdout, "Our table contain %lu\n", table_num_rows);
}
/* Freeing the result */
mysql_free_result(res_num_row);
init_dynamic_string_checked(&query_string, "", 1024, 1024);
/* The rest of the function here */