【MySQL学习】C++外部调用

cpp 复制代码
#include "mysql.h"
MYSQL *mysql;
MYSQL_RES *rec;
MYSQL_ROW row; 

(1)连接

cpp 复制代码
char *server = "localhost";
char *user = "root";
char *password = "hello";
char *database = "mysql";
mysql = mysql_init(NULL);
if(!mysql_real_connect(mysql, server, user, password, database, 0, NULL, 0))
    printf("Connect Failed: %sn\n", mysql_error(mysql));
else
    printf("Connect Succeed ! \n");

(2)运行

cpp 复制代码
string str="show databases;";
if(mysql_real_query(mysql,str.c_str(),str.length()))  
{  
    cout << mysql_error(mysql) << endl;  
    return -1;  
}  

(3)显示结果

cpp 复制代码
rec=mysql_store_result(mysql);
while(row = mysql_fetch_row(rec))  
{  
    for(int i = 0;i < mysql_num_fields(rec);i++)  
    {  
        if(row[i] != NULL)  
            cout << row[i] << "\t";  
        else  
            cout << "<null>" << "\t";  
    }  
    cout << "\n";  
}  

(4)释放记录

cpp 复制代码
mysql_free_result(rec);

(5)关闭连接

cpp 复制代码
mysql_close(mysql);

(6)获取记录数

cpp 复制代码
mysql_num_fields(rec);
相关推荐
OPEN-F4 小时前
C++进阶教程:继承与多态
开发语言·c++
Bruce_Liuxiaowei6 小时前
从基础理论开始学习人工智能(三):盲目搜索——从状态空间图到生成-测试范式
人工智能·学习
luj_17686 小时前
大律师考核应重能力与科技素养
c语言·开发语言·c++·经验分享·算法
醉颜凉6 小时前
KingbaseES日期格式化:MySQL date_format函数的迁移指南
mysql·date_format·kingbasees·数据库兼容·函数迁移
醉颜凉6 小时前
MySQL 8 忘记 root 密码?这两种方法帮你轻松解决
数据库·mysql·adb
有梦想的骇客7 小时前
PyTorch学习笔记
pytorch·笔记·学习
刃神太酷啦7 小时前
Linux 系统 MySQL 完整安装配置教程:从卸载 MariaDB 到优化 my.cnf----《Hello MySQL!》(1)
android·linux·c语言·c++·mysql·leetcode·mariadb
陈年老古董7 小时前
深度学习入门笔记:从神经元到深度神经网络
人工智能·笔记·深度学习·神经网络·学习
xxwl5858 小时前
MySQL 基础学习笔记
数据库·mysql
Rain的Java大神之路8 小时前
PC版网站被狂刷怎么处理
java·数据库·redis·后端·mysql·web安全·运维开发