最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)PyMysql的基本功能實(shí)例詳解

 更新時(shí)間:2020年03月01日 10:39:37   作者:追憶  
這篇文章主要介紹了C++實(shí)現(xiàn)PyMysql的基本功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的工作或?qū)W習(xí)有一定的參考借鑒價(jià)值,需要的朋友可以參考下

用C++實(shí)現(xiàn)一個(gè)Thmysql類,實(shí)現(xiàn)Python標(biāo)準(zhǔn)庫(kù)PyMysql的基本功能,并提供與PyMysql類似的API,并用pybind11將Thmysql封裝為Python庫(kù)。

PyMysql Thmysql(C++) Thmysql(Python)
connect connect connect
cursor —— ——
execute execute execute
fetchone fetchone fetchone
fetchall fetchall fetchall
close close close

一.開(kāi)發(fā)環(huán)境

  • Windows64位操作系統(tǒng);
  • mysql 5.5.28 for Win64(x86);
  • pycharm 2019.1.1。

二.PyMysql數(shù)據(jù)庫(kù)查詢

#文件名:python_test.py
import pymysql
# 連接database
conn = pymysql.connect(host="localhost",user="root",password="123456",
      database="test",charset="utf8")

# 得到一個(gè)可以執(zhí)行SQL語(yǔ)句的光標(biāo)對(duì)象
cursor = conn.cursor() # 執(zhí)行完畢返回的結(jié)果集默認(rèn)以元組顯示
# 執(zhí)行SQL語(yǔ)句
cursor.execute("use information_schema")

cursor.execute("select version();")
first_line = cursor.fetchone()
print(first_line)

cursor.execute("select * from character_sets;")
res = cursor.fetchall()
print(res)
# 關(guān)閉光標(biāo)對(duì)象
cursor.close()
# 關(guān)閉數(shù)據(jù)庫(kù)連接
conn.close()

三.開(kāi)發(fā)步驟

  1. 將mysql安裝目錄下的include和lib文件夾拷到project目錄中;
  2. 將lib文件夾中的libmysql.dll文件拷到system32路徑下;
  3. 定義MysqlInfo結(jié)構(gòu)體,實(shí)現(xiàn)C++版的Thmysql類;
  4. 編寫(xiě)封裝函數(shù);
  5. 通過(guò)setuptools將C++代碼編譯為Python庫(kù)。

四.代碼實(shí)現(xiàn)

// 文件名:thmysql.h
#include <Windows.h>
#include "mysql.h"
#include <iostream>
#include <string>
#include <vector>

#pragma comment(lib, "lib/libmysql.lib")
using namespace std;

typedef struct MysqlInfo{
 string m_host;
 string m_user;
 string m_passwd;
 string m_db;
 unsigned int m_port;
 string m_unix_socket;
 unsigned long m_client_flag;

 MysqlInfo(){}
 MysqlInfo(string host, string user, string passwd, string db, unsigned int port,
     string unix_socket, unsigned long client_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;

class Thmysql{
 public:
  Thmysql();
  void connect(MysqlInfo&);
  void execute(string);
  vector<vector<string>> fetchall();
  vector<string> fetchone();
  void close();
 private:
  MYSQL mysql;
  MYSQL_RES * mysql_res;
  MYSQL_FIELD * mysql_field;
  MYSQL_ROW mysql_row;
  int columns;
  vector<vector<string>> mysql_data;
  vector<string> first_line;
};
// 文件名:thmysql.cpp
#include <iostream>
#include "thmysql.h"

Thmysql::Thmysql(){
 if(mysql_library_init(0, NULL, NULL) != 0){
  cout << "MySQL library initialization failed" << endl;
 }
 if(mysql_init(&mysql) == NULL){
  cout << "Connection handle initialization failed" << endl;
 }
}

void Thmysql::connect(MysqlInfo& msInfo){
 string host = msInfo.m_host;
 string user = msInfo.m_user;
 string passwd = msInfo.m_passwd;
 string db = msInfo.m_db;
 unsigned int port = msInfo.m_port;
 string unix_socket = msInfo.m_unix_socket;
 unsigned long client_flag = msInfo.m_client_flag;

 if(mysql_real_connect(&mysql, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(),
  port, unix_socket.c_str(), client_flag) == NULL){
  cout << "Unable to connect to MySQL" << endl;
 }
}

void Thmysql::execute(string sqlcmd){
 mysql_query(&mysql, sqlcmd.c_str());

 if(mysql_errno(&mysql) != 0){
  cout << "error: " << mysql_error(&mysql) << endl;
 }
}

vector<vector<string>> Thmysql::fetchall(){
 // 獲取 sql 指令的執(zhí)行結(jié)果
 mysql_res = mysql_use_result(&mysql);
 // 獲取查詢到的結(jié)果的列數(shù)
 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<string> row_data;
  for(int i = 0; i < columns; i++){
   if(mysql_row[i] == nullptr){
    row_data.push_back("None");
   }else{
    row_data.push_back(mysql_row[i]);
   }
  }
  mysql_data.push_back(row_data);
 }
 // 沒(méi)有mysql_free_result會(huì)造成內(nèi)存泄漏:Commands out of sync; you can't run this command now
 mysql_free_result(mysql_res);
 return mysql_data;
}

vector<string> Thmysql::fetchone(){
 // 獲取 sql 指令的執(zhí)行結(jié)果
 mysql_res = mysql_use_result(&mysql);
 // 獲取查詢到的結(jié)果的列數(shù)
 columns = mysql_num_fields(mysql_res);
 // 獲取所有的列名
 mysql_field = mysql_fetch_fields(mysql_res);
 first_line.clear();
 mysql_row = mysql_fetch_row(mysql_res);
 for(int i = 0; i < columns; i++){
  if(mysql_row[i] == nullptr){
   first_line.push_back("None");
  }else{
   first_line.push_back(mysql_row[i]);
  }
 }
 mysql_free_result(mysql_res);
 return first_line;
}

void Thmysql::close(){
 mysql_close(&mysql);
 mysql_library_end();
}
// 文件名:thmysql_wrapper.cpp
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
#include "thmysql.h"

namespace py = pybind11;

PYBIND11_MODULE(thmysql, m){
 m.doc() = "C++操作Mysql";
 py::class_<MysqlInfo>(m, "MysqlInfo")
  .def(py::init())
  .def(py::init<string, string, string, string, unsigned int, string, unsigned long>(),
    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_<Thmysql>(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
from setuptools import setup, 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數(shù)據(jù)庫(kù)查詢

#文件名:test.py
from thmysql import Thmysql, MysqlInfo

info = MysqlInfo("localhost", "root", "123456", "", 3306)
conn = Thmysql()
# 連接database
conn.connect(info)
# 執(zhí)行SQL語(yǔ)句
conn.execute("use information_schema")

conn.execute("select version();")
first_line = conn.fetchone()
print(first_line)

conn.execute("select * from character_sets;")
res = conn.fetchall()
print(res)
# 關(guān)閉數(shù)據(jù)庫(kù)連接
conn.close()

 

總結(jié)

到此這篇關(guān)于C++實(shí)現(xiàn)PyMysql的基本功能的文章就介紹到這了,更多相關(guān)c++ pymysql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 華為云CodeArts?IDE?Online快速入門(mén)和使用

    華為云CodeArts?IDE?Online快速入門(mén)和使用

    華為云CodeArts?IDE?Online服務(wù),提供了可隨時(shí)隨地編碼的云上開(kāi)發(fā)環(huán)境,同時(shí)具備開(kāi)放的生態(tài)和獨(dú)立插件市場(chǎng),本文主要介紹了華為云CodeArts?IDE?Online快速入門(mén)和使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • C語(yǔ)言實(shí)現(xiàn)火車訂票系統(tǒng)

    C語(yǔ)言實(shí)現(xiàn)火車訂票系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)火車訂票系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 詳解C++ sizeof(上)

    詳解C++ sizeof(上)

    這篇文章主要介紹了C++ sizeof的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下
    2020-08-08
  • C++實(shí)現(xiàn)defer聲明方法詳解

    C++實(shí)現(xiàn)defer聲明方法詳解

    這篇文章主要介紹了C++實(shí)現(xiàn)defer聲明,在和朋友交談時(shí)候,無(wú)意間了解到Go語(yǔ)言的defer,發(fā)現(xiàn)挺有意思的。和智能指針類似,當(dāng)出了作用域后,被defer修飾的操作才會(huì)執(zhí)行
    2022-11-11
  • C++實(shí)現(xiàn)完整功能的通訊錄管理系統(tǒng)詳解

    C++實(shí)現(xiàn)完整功能的通訊錄管理系統(tǒng)詳解

    來(lái)了來(lái)了,通訊錄管理系統(tǒng)踏著七彩祥云飛來(lái)了,結(jié)合前面的結(jié)構(gòu)體知識(shí)和分文件編寫(xiě)方法,我總結(jié)并碼了一個(gè)帶菜單的通訊錄管理系統(tǒng),在這篇文章中將會(huì)提到C的清空屏幕函數(shù),嵌套結(jié)構(gòu)體具體實(shí)現(xiàn),簡(jiǎn)單且充實(shí),跟著我的思路,可以很清晰的解決這個(gè)項(xiàng)目
    2022-05-05
  • 基于OpenCV?差分法實(shí)現(xiàn)綠葉識(shí)別

    基于OpenCV?差分法實(shí)現(xiàn)綠葉識(shí)別

    物體識(shí)別是圖像處理學(xué)在現(xiàn)實(shí)生活中較多的應(yīng)用之一,本文提供了一種相對(duì)簡(jiǎn)單的思路來(lái)實(shí)現(xiàn)綠葉識(shí)別,適合初學(xué)圖像處理的新人研究參考。感興趣的同學(xué)可以關(guān)注一下
    2021-11-11
  • c++中八大排序算法

    c++中八大排序算法

    本篇文章主要介紹了八大排序算法,詳細(xì)的介紹了八個(gè)算法思想,實(shí)現(xiàn)代碼,穩(wěn)定性,時(shí)間復(fù)雜度等,具有一定的參考價(jià)值,有需要的可以了解一下。
    2016-11-11
  • VC++在TXT文件指定位置追加內(nèi)容的方法

    VC++在TXT文件指定位置追加內(nèi)容的方法

    這篇文章主要介紹了VC++在TXT文件指定位置追加內(nèi)容的方法,功能較為實(shí)用,需要的朋友可以參考下
    2014-08-08
  • 基于C++實(shí)現(xiàn)的哈夫曼編碼解碼操作示例

    基于C++實(shí)現(xiàn)的哈夫曼編碼解碼操作示例

    這篇文章主要介紹了基于C++實(shí)現(xiàn)的哈夫曼編碼解碼操作,結(jié)合實(shí)例形式分析了C++實(shí)現(xiàn)的哈夫曼編碼解碼相關(guān)定義與使用技巧,需要的朋友可以參考下
    2018-04-04
  • C++的多態(tài)與虛函數(shù)你了解嗎

    C++的多態(tài)與虛函數(shù)你了解嗎

    這篇文章主要為大家詳細(xì)介紹了C++多態(tài)與虛函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03

最新評(píng)論

永平县| 宿松县| 铜陵市| 汨罗市| 梓潼县| 宁阳县| 麦盖提县| 岢岚县| 加查县| 张掖市| 曲阳县| 衡阳县| 盈江县| 天峨县| 榆社县| 弥渡县| 龙南县| 温州市| 万荣县| 蓬溪县| 界首市| 红河县| 萍乡市| 调兵山市| 临城县| 怀柔区| 宁波市| 犍为县| 甘南县| 金乡县| 铜梁县| 抚顺市| 大冶市| 虞城县| 富平县| 彩票| 莒南县| 新巴尔虎右旗| 抚顺市| 右玉县| 清水河县|