方法1:
// mysqlTest.cpp : 定义控制台应用程序的入口点。 //64 #include "stdafx.h" #include <mysql++.h> #include <ssqls.h> #include <sstream> sql_create_2(test, 1, 2, mysqlpp::sql_int, id, mysqlpp::sql_blob_null, value) struct SCurScoutList { struct SScoutInList { SScoutInList(): m_nScoutID(0), m_bSigned(0) {} int m_nScoutID; byte m_bSigned; }; SCurScoutList() {} SScoutInList m_scoutList[8]; }; int main() { SCurScoutList mylist; SCurScoutList mylist1; mylist.m_scoutList[0].m_nScoutID = 106100043; mylist.m_scoutList[1].m_nScoutID = 106100046; mylist.m_scoutList[2].m_nScoutID = 106100050; mylist.m_scoutList[3].m_nScoutID = 106100035; mylist.m_scoutList[4].m_nScoutID = 106100020; mylist.m_scoutList[5].m_nScoutID = 106100035; mylist.m_scoutList[6].m_nScoutID = 106100005; mylist.m_scoutList[7].m_nScoutID = 106100011; mylist.m_scoutList[7].m_bSigned = 111; try { mysqlpp::Connection conn(true); const std::string db="game"; if (!conn.connect(db.c_str(), "127.0.0.1", "xt", "111111")) { return 0; } mysqlpp::Query query = conn.query(); query << "select * from test where id = 1"; query.parse(); mysqlpp::StoreQueryResult res = query.store(); test myblob1 = res[0]; memcpy(&mylist1, myblob1.value.data, myblob1.value.data.length()); test myblob2 = myblob1; myblob2.value.data.assign((const char*)&mylist, sizeof(mylist)); query.update(myblob1, myblob2); query.execute(); } catch(const mysqlpp::Exception& er) { std::cout << er.what() << std::endl; } getchar(); return 0; }
方法2:
这个方法在debug下会有断言
#include "stdafx.h" #include <mysql++.h> #include <ssqls.h> #include <sstream> sql_create_2(test, 1, 2, mysqlpp::sql_int, id, mysqlpp::sql_blob_null, value) struct SCurScoutList { struct SScoutInList { SScoutInList(): m_nScoutID(0), m_bSigned(0) {} int m_nScoutID; byte m_bSigned; }; SCurScoutList() {} SScoutInList m_scoutList[8]; }; int main() { SCurScoutList mylist; SCurScoutList mylist1; mylist.m_scoutList[0].m_nScoutID = 106100043; mylist.m_scoutList[1].m_nScoutID = 106100046; mylist.m_scoutList[2].m_nScoutID = 106100050; mylist.m_scoutList[3].m_nScoutID = 106100035; mylist.m_scoutList[4].m_nScoutID = 106100020; mylist.m_scoutList[5].m_nScoutID = 106100035; mylist.m_scoutList[6].m_nScoutID = 106100005; mylist.m_scoutList[7].m_nScoutID = 106100011; mylist.m_scoutList[7].m_bSigned = 113; std::string strData; strData.assign(reinterpret_cast<const char*>(&mylist), sizeof(mylist)); char szbuffer[2046] = {0}; memcpy(szbuffer, (const char*)&mylist, sizeof(mylist)); try { mysqlpp::Connection conn(true); const std::string db="game"; if (!conn.connect(db.c_str(), "127.0.0.1", "xt", "111111")) { return 0; } mysqlpp::Query query = conn.query(); query << "update test set value = "" << mysqlpp::escape << strData << "" where id = " << 1; query.parse(); query.execute(); query.reset(); query << "Select id, value from test where id=1"; query.parse(); mysqlpp::StoreQueryResult res = query.store(); int nRowNum = res.num_rows(); mysqlpp::sql_blob_null blobScoutList = res[0]["value"]; int nBlobLen = blobScoutList.data.length(); int nsizeof = sizeof(SCurScoutList); if(nBlobLen == sizeof(SCurScoutList)) { memcpy(&mylist1, blobScoutList.data.data(), nBlobLen); } } catch(const mysqlpp::Exception& er) { std::cout << er.what() << std::endl; } getchar(); return 0; }