Share the encapsulated asynchronous Mysql dynamic library (DyNetMysql. dll) + project source code, and mysql asynchronous
Mysql databases are often used for C ++ projects. The data provided by Mysql interfaces is quite primitive, such as field names, field types, and field lengths, generally, we want to obtain data more easily.
For example:
XXXStruct * pRecourdInfo = (const XXXStruct *) (Data Pointer obtained by mysql );
Directly convert the structure of the field to make it easier for us to use
This function has been implemented in my previous article <shared with the simple, practical, and efficient C ++ Socket framework Swa-server (Open Source + Source Code)>, now it is encapsulated into a dll
Recently, I learned about dll and encapsulated it into a dll to facilitate use and transplantation to other projects. The code of the mysql asynchronous processing function in swa-server is separated, and a DyNetMysql project is created and set as a dll project.
For dll project and export, do not describe it here. yuanyou queries Baidu (zf has blocked Google)
Add the code (* _ *) Based on the personality of the park friends (*_*)
// 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 {int64 type_idx; char nick [32]; int32 level ;}; # pragma pack (pop) void DBCallbase (void * pUser, const void * pMsg) {const DbRecordSet * pRecordSet = static_cast <const DbRecordSet *> (pMsg ); for (int32 I = 0; I <pRecordSet-> CountRecord (); ++ I) {const CharacterInfo * pInfo = static_cast <const CharacterInfo *> (pRecordSet-> GetRecordBuff (I); printf ("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 ); // 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 ;}
Start creating a Mysql connection object here. If yes, return! Null, and then the SQL statement can be executed asynchronously and the callback can be set (callback can be left unspecified)
The above code runs as follows (Why is printf used? If the character is placed at a second or higher, it is <null>. If you know why, LZ is grateful ):
In fact, it is not difficult to directly convert the data into a struct, that is, each row is given to a char buffer [65536], then the mysql field memory is copied, and then converted into a struct.
Note that if it is a char or varchar field, You Need To Know What character encoding mysql uses when you transfer it out. If it is UTF-8, the maximum byte length is the defined length * 3, dynetmysql adopts UTF-8 and the default length is the byte length. Therefore, when storing data in Chinese, note that three Chinese characters are used as the length.
Code 1 of related classes:
DbRecordSet & m_rRecordSet; vector <DbField> m_vecFields; char m_arrRowBuff [65536]; // records the content of each row. Each field records its starting position, the maximum number of records in a row is 64 k uint32 m_nBuffBegin; // The position in use.
Code 2 of related classes:
Switch (nType) {case FIELD_TYPE_TINY: {mI64Val = pszValue? Atol (pszValue): 0; memcpy (& m_arrRowBuff [m_nBuffBegin], & mI64Val, 1); m_nBuffBegin + = 1;} break; case FIELD_TYPE_SHORT: {mI64Val = pszValue? Atol (pszValue): 0; memcpy (& m_arrRowBuff [m_nBuffBegin], & mI64Val, 2); m_nBuffBegin + = 2;} break; case FIELD_TYPE_LONG: {mI64Val = pszValue? Atol (pszValue): 0; memcpy (& m_arrRowBuff [m_nBuffBegin], & mI64Val, 4); m_nBuffBegin + = 4;} break; case FIELD_TYPE_FLOAT: {mF64Val = pszValue? Atof (pszValue): 0; memcpy (& m_arrRowBuff [m_nBuffBegin], & mF64Val, 4); m_nBuffBegin + = 4;} break; case FIELD_TYPE_LONGLONG: {mI64Val = pszValue? Atol (pszValue): 0; memcpy (& m_arrRowBuff [m_nBuffBegin], & mI64Val, 8); m_nBuffBegin + = 8;} break; case FIELD_TYPE_DOUBLE: {mF64Val = pszValue? Atof (pszValue): 0; memcpy (& m_arrRowBuff [m_nBuffBegin], & mF64Val, 8); m_nBuffBegin + = 8;} break; case FIELD_TYPE_STRING: // char case FIELD_TYPE_VAR_STRING: // var_char {memcpy (& m_arrRowBuff [m_nBuffBegin], pszValue, nLen/3); // The mysql char size is calculated based on the maximum length, here we limit the truncation of m_nBuffBegin + = nLen/3;} break; case when: case MYSQL_TYPE_BLOB: {memcpy (& m_arrRowBuff [m_nBuffBegin], pszValue, nLen); m_nBuffBegin + = nLen;} break;
Currently, this DyNetMysql. the dll is not complete yet. If it is a direct select statement, you can create a struct that corresponds to it, and then directly convert it to the struct. If it is another such as update or delete, we recommend that you do not use callback or stored procedures.
The test code also uses the dll that recently encapsulates an asynchronous Socket. If you are interested,Leave a message and recommend it.I have the motivation to share it with my friends! If you have any questions, you can leave a message or qq296464231 to ask me.
Download project source code