我想添加一个注释,为MySQLDump输出每个表转储的行数。为此,我需要修改文件client/mysqldump.c。输出示例如下:
-- Table structure for table t1
DROP TABLE IF EXISTS `t1`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `t1` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
--转储表t1
的数据
锁定表格`t1`WRITE;/*!40000 ALTER TABLE `t1`DISABLE KEYS*/;插入"t1"值(5);/*!40000 ALTER TABLE `t1`ENABLE KEYS*/;解锁表格;/*!40103设置TIME_ZONE=@OLD_TIME_ZONE*/;--找到"t1"的行:1
我不怎么使用MySQL,也不明白该如何解决这个问题。如有任何帮助,我们将不胜感激。谢谢
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' %sn", 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 %lun", table_num_rows)
}
关于如何使用MYSQL C API的完整示例,您可以在这里阅读答案:使用c 从单板计算机写入mysql数据库
EDIT:当我们想要转储任何表(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 usedn",
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 %sn", 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 fieldsn",
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 usedn");
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 - %sn", 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 %lun", 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 */