mysql dump 40101,对于MySQLDump,添加一个注释,其中输出每个表的转储行数

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的完整示例,您可以在此处阅读答案:

Writing into mysql database from a single board computer using c

编辑:当我们想要转储任何表(mysqldump.c)时,似乎使用了dump_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 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值