2

我想添加一条注释,输出为 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 */;

--

-- Dumping data for table t1

LOCK TABLES `t1` WRITE;

/*!40000 ALTER TABLE `t1` DISABLE KEYS */;

INSERT INTO `t1` VALUES (5);

/*!40000 ALTER TABLE `t1` ENABLE KEYS */;

UNLOCK TABLES;

/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

-- Rows found for 't1':  1

我没有太多使用 MySQL,也不明白我应该如何着手解决这个问题。任何帮助,将不胜感激。谢谢!

4

1 回答 1

2

MYSQL C API 有一个函数可以返回请求返回的行数mysql_num_rowshttp://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

编辑:似乎dump_table当我们想要转储任何表(mysqldump.c)时使用该函数,这是添加我们的代码来计算行数的好地方。像这样修改它(你需要做一些测试,我没有在我的机器上测试过代码!):

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 */
于 2012-09-01T22:35:14.697 回答