Share the packaged asynchronous MySQL dynamic library (DyNetMysql.dll) + Project source code

Source: Internet
Author: User

In the C + + project, often use the MySQL database, MySQL interface provides us with the data is quite primitive, such as: Field name, field type, field length and so on, generally we want a more convenient access to data

Such as:

xxxstruct* Precourdinfo = (const xxxstruct*) (data pointer obtained by MySQL);

Direct conversion of the corresponding structure of the field, so that we can greatly facilitate the use of

This feature has been published in my previous article << share has been implemented in the maintenance of simple and efficient C++socket framework swa-server (open source + source code) >>, now it is encapsulated as a DLL

Recently a bit of time to learn about DLL knowledge, and then encapsulated into a DLL, so that easy to use and porting to other projects. The MySQL asynchronous processing function code in Swa-server is then separated to create a Dynetmysql project, set as a DLL project

About DLL projects and exports do not tell here, park friends themselves Baidu query (ZF put Google sealed really served)

According to the park friend character on the first code (*_*)

TestDll.cpp: Defines the entry point of the console application. #include "stdafx.h" #include ". /.. /dynetserver/netserver.h "#include". /.. /dynetmysql/dbsession.h "#include". /.. /dynetmysql/dbrecordset.h "#include <iostream>using namespace std; Netserver*gnetserver;idbsession*gdatabase;void Onnetmsgenter (netsocket& rsocket); void OnNetMsgExit (NetSocket & Rsocket), void Onnetmsg (netsocket& rsocket, netmsghead* phead), #pragma pack (push,1) struct CHARACTERINFO{INT6 4 Type_idx;char nick[32];int32 level;}; #pragma pack (pop) void Dbcallbase (void* puser, const void* PMSG) {const dbrecordset* precordset = Static_cast<const DbRe Cordset*> (PMSG); for (Int32 i = 0; i < Precordset->countrecord (); ++i) {const characterinfo* pInfo = STATIC_CAST&L T;const characterinfo*> (Precordset->getrecordbuff (i));p rintf ("id:%ld nick:%s level:%d \ n", PInfo->type_idx , pinfo->nick,pinfo->level);}} int _tmain (int argc, _tchar* argv[]) {//Start Msyqlgdatabase = Dbsessionmgr::instance ()->adddatabase ("127.0.0.1", "RoOT "," root "," Swa_data ", false); ASSERT (gdatabase);//Initialize Scoket service gnetserver = Netservermgr::instance ()->addnetserver ();gnetserver-> Setaddress ("127.0.0.1", 4321); Gnetserver->sethandler (Onnetmsgenter, onnetmsg, onnetmsgexit);// Start the Socket service Gnetserver->start (); Gdatabase->executeasyncsql ("Select ' Type_idx ', ' Nick ', ' Level ' from ' character ') ", null,dbcallbase); while (true) {Sleep (1); Netservermgr::instance ()->update (0); Logic processing dbsessionmgr::instance ()->update (0); SQL processing and callback}return 0;} void Onnetmsgenter (netsocket& rsocket) {int a = 1;} void Onnetmsg (netsocket& rsocket, netmsghead* phead) {int a = 1;} void Onnetmsgexit (netsocket& rsocket) {int a = 1;}

This starts the creation of a MySQL connection object, and if!null is successfully returned, then SQL and set callbacks can be executed asynchronously (without setting callbacks)

The above code runs as follows (why with printf, if the character is placed above the second display as <null> If you know what is the reason, LZ grateful):

In fact, it is not difficult to implement the data directly to the structure, that is, each line to a char buffer[65536], and then copy the MySQL field memory, and then to the structure can be

Note here, if it is a char or varchar field, turn out to know what the MySQL is the character encoding, if it is utf-8 byte maximum length is defined length of a, this dynetmysql is pressed utf-8, and the length is the default byte length, So the storage is Chinese to note that a Chinese is used with 3 lengths.

Related class section Code 1:

    dbrecordset&    m_rrecordset;    Vector<DbField>    m_vecfields;     Char            m_arrrowbuff[65536//    uint32            //  location already in use

Related class Section Code 2:

    Switch(nType) { Casefield_type_tiny: {mi64val= Pszvalue? Atol (pszvalue):0; memcpy (&m_arrrowbuff[m_nbuffbegin], &mi64val,1); M_nbuffbegin+=1; }         Break;  Casefield_type_short: {mi64val= Pszvalue? Atol (pszvalue):0; memcpy (&m_arrrowbuff[m_nbuffbegin], &mi64val,2); M_nbuffbegin+=2; }         Break;  CaseField_type_long: {mi64val= Pszvalue? Atol (pszvalue):0; memcpy (&m_arrrowbuff[m_nbuffbegin], &mi64val,4); M_nbuffbegin+=4; }         Break;  Casefield_type_float: {mf64val= Pszvalue? Atof (pszvalue):0; memcpy (&m_arrrowbuff[m_nbuffbegin], &mf64val,4); M_nbuffbegin+=4; }         Break;  CaseField_type_longlong: {mi64val= Pszvalue? Atol (pszvalue):0; memcpy (&m_arrrowbuff[m_nbuffbegin], &mi64val,8); M_nbuffbegin+=8; }         Break;  Casefield_type_double: {mf64val= Pszvalue? Atof (pszvalue):0; memcpy (&m_arrrowbuff[m_nbuffbegin], &mf64val,8); M_nbuffbegin+=8; }         Break;  CaseField_type_string://Char     CaseField_type_var_string://Var_char{memcpy (&m_arrrowbuff[m_nbuffbegin], pszvalue, Nlen/3);//the MySQL char definition size calculates the length according to the maximum length, which limits the size of bytes to truncateM_nbuffbegin + = Nlen/3; }         Break;  CaseMysql_type_tiny_blob: CaseMysql_type_medium_blob: CaseMysql_type_long_blob: CaseMysql_type_blob: {memcpy (&M_arrrowbuff[m_nbuffbegin], pszvalue, Nlen); M_nbuffbegin+=Nlen; }         Break;

At present, this DyNetMysql.dll is not perfect, if it is direct select can create a struct and its corresponding can be directly to the structure, if it is other such as Update,delete, it is recommended to set not to use callbacks or use stored procedures.

Test code also used in the recent encapsulation of an asynchronous socket DLL, if you are interested, please leave a message and recommend it , I will have the power to share with the garden friends! If there is a problem, you can leave a message or qq296464231 ask me

Project Source Download

Share the packaged asynchronous MySQL dynamic library (DyNetMysql.dll) + Project source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.