用C++实现一个Thmysql类,实现Python标准库PyMysql的基本功能,并提供类似于PyMysql的API,使用pybind11将Thmysql打包为Python库。PyMysqlThmysql(C++)Thmysql(Python)connectconnectconnectconnectcursor——executeexecuteexecutefetchonefetchonefetchonefetchallfetchallfetchallcloseclose1、开发环境Windows64位操作系统;用于Win64(x86)的mysql5.5.28;pycharm2019.1.1.2.PyMysql数据库查询#文件名:python_test.pyimportpymysql#connectdatabaseconn=pymysql.connect(host="localhost",user="root",password="123456",database="test",charset="utf8")#获取一个可以执行SQL语句的游标对象cursor=conn.cursor()#执行后返回的结果集默认以元组显示#执行SQL语句cursor.execute("useinformation_schema")cursor.execute("selectversion();")first_line=cursor.fetchone()print(first_line)cursor.execute("select*fromcharacter_sets;")res=cursor.fetchall()print(res)#关闭游标对象cursor.close()#关闭数据库连接conn.close()三、开发步骤将mysql安装目录下的include和lib文件夹复制到项目目录下;将lib文件夹下的libmysql.dll文件复制到system32路径下;定义MysqlInfo结构来实现Thmysql类的C++版本;编写包装函数;通过setuptools将C++代码编译成Python库。四.代码实现//文件名:thmysql.h#include#include"mysql.h"#include#include#include#pragmacomment(lib,"lib/libmysql.lib")使用命名空间std;typedefstructMysqlInfo{stringm_host;字符串m_user;字符串m_passwd;字符串m_db;无符号整数m_port;字符串m_unix_socket;无符号长m_client_flag;MysqlInfo(){}MysqlInfo(stringhost,stringuser,stringpasswd,stringdb,unsignedintport,stringunix_socket,unsignedlongclient_flag){m_host=host;m_user=用户;m_passwd=密码;m_db=分贝;m_port=端口;m_unix_socket=unix_socket;m_client_flag=client_flag;}}MysqlInfo;classThmysql{public:Thmysql();无效连接(MysqlInfo&);无效执行(字符串);矢量<矢量<字符串>>fetchall();矢量<字符串>fetchone();无效关闭();私有:MYSQLmysql;MYSQL_RES*mysql_res;MYSQL_FIELD*mysql_field;MYSQL_ROWmysql_row;整数列;矢量<矢量<字符串>>mysql_data;vectorfirst_line;};//文件名:thmysql.cpp#include#include"thmysql.h"Thmysql::Thmysql(){if(mysql_library_init(0,NULL,NULL)!=0){cout<<"MySQL库初始化失败"<>Thmysql::fetchall(){//获取sql指令的执行结果mysql_res=mysql_use_result(&mysql);//获取查询到的结果的列表columns=mysql_num_fields(mysql_res);//获取所有的列表名mysql_field=mysql_fetch_fields(mysql_res);mysql_data.clear();while(mysql_row=mysql_fetch_row(mysql_res)){vectorrow_data;for(inti=0;iThmysql::fetchone(){//获取sql命令的执行结果mysql_res=mysql_use_result(&mysql);//获取查询结果的列数columns=mysql_num_fields(mysql_res);//获取所有列名mysql_field=mysql_fetch_fields(mysql_res);first_line.clear();mysql_row=mysql_fetch_row(mysql_res);for(inti=0;i(m,"MysqlInfo").def(py::init()).def(py::init(),py::arg(“主机”),py::arg(“用户”),py::arg(“passwd”),py::arg(“db”),py::arg(“端口”),py::arg("unix_socket")="NULL",py::arg("client_flag")=0).def_readwrite("host",&MysqlInfo::m_host).def_readwrite("user",&MysqlInfo::m_user).def_readwrite("passwd",&MysqlInfo::m_passwd).def_readwrite("db",&MysqlInfo::m_db).def_readwrite("端口",&MysqlInfo::m_port).def_readwrite("unix_socket",&MysqlInfo::m_unix_socket).def_readwrite("client_flag",&MysqlInfo::m_client_flag);py::class_(m,"Thmysql").def(py::init()).def("连接",&Thmysql::connect).def("执行",&Thmysql::执行,py::arg("sql_cmd")).def("fetchall",&Thmysql::fetchall)。def("fetchone",&Thmysql::fetchone).def("close",&Thmysql::close);}#文件名:setup.pyfromsetuptoolsimportsetup,Extensionfunctions_module=Extension(name='thmysql',sources=['thmysql.cpp','thmysql_wrapper.cpp'],include_dirs=[r'D:\software\pybind11-master\include',r'D:\software\Anaconda\include',r'D:\project\thmysql\include'],)setup(ext_modules=[functions_module])五.Thmysql数据库查询#文件名:test.pyfromthmysqlimportThmysql,MysqlInfoinfo=MysqlInfo("localhost","root","123456","",3306)conn=Thmysql()#连接数据库conn.connect(info)#执行SQL语句conn.execute("useinformation_schema")conn.execute("selectversion();")first_line=conn.fetchone()print(first_line)conn.execute("select*fromcharacter_sets;")res=conn.fetchall()print(res)#关闭数据库连接conn.close()