Mysql 8.0 C API连接和获取数据实例(附解决乱码的彩蛋)

假装前言

使用Mysql的C API,编写一个连接Mysql数据库实例的代码。

安装Mysql C API

这个有点麻烦,如果Windows平台下的Mysql Installer的话,安装个Mysql Server。相关的头件,库文件在如下 的目录下面。
Mysql Server安装后的相关目录结构

实例代码

人好话不多,直接上代码。

#include <iostream>
#include <assert.h>
#include "mysql.h"
#pragma comment(lib, "libmysql.lib")

int main(int argc, char* argv[])
{
    MYSQL *pMysql = mysql_init(NULL);
    if ( NULL == pMysql )
    {
        printf("%s: %d : error : %s\n",__FILE__,  __LINE__, mysql_error(pMysql));
        exit(1);
    }
    if ( NULL == mysql_real_connect(pMysql, "x.x.x.x", "user", "password", "database", 3306, NULL, 0) )
    {
        printf("%s: %d : error : %s\n", __FILE__, __LINE__, mysql_error(pMysql));
        exit(1);
    }
    if ( NULL != mysql_set_character_set(pMysql, "utf8") )
    {
        printf("%s: %d : error : %s\n", __FILE__, __LINE__, mysql_error(pMysql));
        exit(1);
    }
    if ( NULL != mysql_real_query(pMysql, "select * from sys_user", strlen("select * from sys_user")) )
    {
        printf("%s: %d : error : %s\n", __FILE__, __LINE__, mysql_error(pMysql));
        exit(1);
    }
    MYSQL_RES* res = mysql_store_result(pMysql);
    if (NULL == res )
    {
        printf("%s : %d : error : %s\n", __FILE__, __LINE__, mysql_error(pMysql));
        exit(1);
    }

    // Handling result set retrieved from database table.
 
    // Retrieve meta data information.
    MYSQL_FIELD* myfields;
    while (myfields = mysql_fetch_field(res))
    {
        printf("%s", myfields->name);
        printf(" | ");
    }
    printf("\n"); 
    printf("--------------------------------------------------------------------------------------------------------\n");

    // Retrieve cell value of each row.
    int cols = 0;
    cols = mysql_num_fields(res);
    MYSQL_ROW row;
    while ( row = mysql_fetch_row(res) )
    {
        for ( int i = 0; i < cols; i++ )
        {
            if (row[i] != NULL)
            {
                // To display correctly. use chcp 65001.
                SetConsoleOutputCP(65001);
                size_t nSize = strlen(row[i]);
                char*pC = (char*)malloc(sizeof(char) * nSize + 2);
                assert(pC != NULL);
                memset(pC, 0, nSize+2);
                strncpy_s(pC, nSize+2, row[i], 5120);
                printf("%s", row[i]);
            }
            else
            {
                printf("NULL");
            }

            printf("|");
        }
        printf("\n");
    }

    mysql_free_result(res);
    mysql_close(pMysql);
}

文末彩蛋,解决prinf打印乱码的问题

开始调试和运行后,发现中文字符内容在console中,打印出来的一直是乱码,即使设置了mysql_set_character_set(pMysql, "utf8")。定位后发现,Mysql C API在获取结果时,通过printf打印乱码的原因是,用于输出的console控制台的code page不一致导致无法正确显示UTF8编码的内容。按以下方式(仅在windows平台),设置console的code page代码页,即可正常显示中文字符。

// To display correctly. use chcp 65001.
SetConsoleOutputCP(65001);

Code Pages On Microsoft Learn: link

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

招财猫_Martin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值