C++实现PyMysql的基本功能实例详解
用C++实现一个Thmysql类,实现Python标准库PyMysql的基本功能,并提供与PyMysql类似的API,并用pybind11将Thmysql封装为Python库。
PyMysql | Thmysql(C++) | Thmysql(Python) |
---|---|---|
connect | connect | connect |
cursor | —— | —— |
execute | execute | execute |
fetchone | fetchone | fetchone |
fetchall | fetchall | fetchall |
close | close | close |
一.开发环境
- Windows64位操作系统;
- mysql5.5.28forWin64(x86);
- pycharm2019.1.1。
二.PyMysql数据库查询
#文件名:python_test.py importpymysql #连接database conn=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文件夹拷到project目录中;
- 将lib文件夹中的libmysql.dll文件拷到system32路径下;
- 定义MysqlInfo结构体,实现C++版的Thmysql类;
- 编写封装函数;
- 通过setuptools将C++代码编译为Python库。
四.代码实现
//文件名:thmysql.h #include#include"mysql.h" #include #include #include #pragmacomment(lib,"lib/libmysql.lib") usingnamespacestd; typedefstructMysqlInfo{ stringm_host; stringm_user; stringm_passwd; stringm_db; unsignedintm_port; stringm_unix_socket; unsignedlongm_client_flag; MysqlInfo(){} MysqlInfo(stringhost,stringuser,stringpasswd,stringdb,unsignedintport, stringunix_socket,unsignedlongclient_flag){ m_host=host; m_user=user; m_passwd=passwd; m_db=db; m_port=port; m_unix_socket=unix_socket; m_client_flag=client_flag; } }MysqlInfo; classThmysql{ public: Thmysql(); voidconnect(MysqlInfo&); voidexecute(string); vector >fetchall(); vector fetchone(); voidclose(); private: MYSQLmysql; MYSQL_RES*mysql_res; MYSQL_FIELD*mysql_field; MYSQL_ROWmysql_row; intcolumns; vector >mysql_data; vector first_line; }; //文件名:thmysql.cpp #include #include"thmysql.h" Thmysql::Thmysql(){ if(mysql_library_init(0,NULL,NULL)!=0){ cout<<"MySQLlibraryinitializationfailed"< >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)){ vector row_data; for(inti=0;i Thmysql::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 //文件名:thmysql_wrapper.cpp #include"pybind11/pybind11.h" #include"pybind11/stl.h" #include"thmysql.h" namespacepy=pybind11; PYBIND11_MODULE(thmysql,m){ m.doc()="C++操作Mysql"; py::class_(m,"MysqlInfo") .def(py::init()) .def(py::init (), py::arg("host"),py::arg("user"),py::arg("passwd"),py::arg("db"),py::arg("port"), 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("port",&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("connect",&Thmysql::connect) .def("execute",&Thmysql::execute,py::arg("sql_cmd")) .def("fetchall",&Thmysql::fetchall) .def("fetchone",&Thmysql::fetchone) .def("close",&Thmysql::close); } #文件名:setup.py fromsetuptoolsimportsetup,Extension functions_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.py fromthmysqlimportThmysql,MysqlInfo info=MysqlInfo("localhost","root","123456","",3306) conn=Thmysql() #连接database 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()总结
到此这篇关于C++实现PyMysql的基本功能的文章就介绍到这了,更多相关c++pymysql内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。