📄 db_mysql.cpp
字号:
//int any; //int iFrom; int nCount=0; //strncpy(QueryKey,Commandstr,6); if( (strncmp(CommandStr,"SELECT",6)!=0) && (strncmp(CommandStr,"select",6)!=0) ) { printf("DBquery:::Not 'Select' Command!"); return 1; } /*if(CommandStr[7]=='*') any = 1; else any = 0; iFrom = strstr(CommandStr,"from"); if(iFrom==NULL) iFrom = strstr(CommandStr,"FROM");*/ if(mysql_query(&conn,CommandStr)) { fprintf(stderr,"DBquery::: Failed: %s\n", mysql_error(&conn)); return 1; } res = mysql_store_result(&conn); while((row = mysql_fetch_row(res))) { //if( (strncmp(&CommandStr[iFrom + 6],"Session",7)==0) && (any==1)) /*查session表*/ if(DataType==1) /*查session表,返回Session结构体数组*/ { } //else if(!any) /*查询char型单字段,返回字符串(二维)数组*/ else if(DataType==2) { char (*temp)[][STRINGLEN]; //二维数组的指针 (char *)temp = (char *)RecordStruct; strcpy((*temp)[nCount], row[0]); (*temp)[nCount][strlen(row[0])]='\0'; //strcpy( &(((char *)RecordStruct)[nCount]),row[0]); } else if(DataType==3) /*查询int型单字段,返回一维数组*/ { ((int *)RecordStruct)[nCount] = atoi(row[0]); } nCount++; if(nCount==*RecordNum) break; //已经得到期望的记录数,不再取多余的记录,以免写溢出 } *RecordNum = mysql_num_rows(res); //返回查询所得记录数 mysql_free_result(res); return 0;}/*===========================================================================------------------------------执行数据库增删改语句----------------------------=============================================================================*/int DB_MySQL::DBModify(char * CommandStr){ SafeQuery(CommandStr); return mysql_affected_rows(&conn);}/*===========================================================================--------------------------从配置文件获取数据库操作参数-----------------------=============================================================================*/int DB_MySQL::DBInit(char * ConfigFile){ strcpy(DBConfig,ConfigFile); if(ConfigFile==NULL || strlen(ConfigFile)==0) { printf("\n DBInit: ConfigFile[%s] for Database is not correct!\n",ConfigFile); return 1; } fp = new FileOpr(); assert(fp!=NULL); fp->RWOpen(DBConfig,"r"); memset(DBHost,0,32); memset(DBUser,0,32); memset(DBPass,0,32); memset(DBName,0,32); fp->RWRead("DB_HOST",DBHost); fp->RWRead("DB_USER",DBUser); fp->RWRead("DB_PASS",DBPass); fp->RWRead("DB_NAME",DBName);// printf("host=%s\n",DBHost);// printf("user=%s\n",DBUser);// printf("pass=%s\n",DBPass);// printf("name=%s\n",DBName); fp->RWClose(); delete fp; return 0;}/*===========================================================================---------------------------------建立数据库连接------------------------------=============================================================================*/int DB_MySQL::DBConn(){ if(!mysql_init(&conn)) { printf("\n DBInit: Unable to initialize MYSQL struct! \n"); return 0; } //MYSQL4.0以上版本用此段连接代码 if(mysql_real_connect(&conn,DBHost,DBUser,DBPass,DBName,3306,NULL,0)) { cout<< "\n DBConn: DB["<< DBName <<"] is connected successfully!"<< endl; }else{ sprintf(errmsg,"\n mysql_real_connect() Failed: %s\n",mysql_error(&conn)); cout<< errmsg << endl; printf("\nDBConfig=%s,Host=%s,User=%s,passwd=%s,db=%s,\n",DBConfig,DBHost,DBUser,DBPass,DBName); return 1; } /* //MYSQL4.0以下版本用此段连接代码 if(mysql_connect(&conn,DBHost,DBUser,DBPass)) if(mysql_select_db(&conn, DBName)==0) { cout<< "sms is selected successfully!"<< endl; }else{ sprintf(errmsg,"mysql_select_db() Failed: %s\n", mysql_error(&conn)); cout<<errmsg; return 1; } */ return 0;}/*===========================================================================---------------------------------关闭数据库连接------------------------------=============================================================================*/int DB_MySQL::DBClose(){ mysql_close(&conn); return 0;}/*===========================================================================--------------------------传入已初始化的数据库连接---------------------------=============================================================================*/int DB_MySQL::GetConn(MYSQL *Connection){ if(!Connection) { conn = *Connection; return 0; } else return 1;}/*========================================================================================================================================================*/char* DB_MySQL::getTime(char *szbuf){ time_t tt; struct tm *now; time(&tt); now = localtime( &tt); sprintf( szbuf, "%02d%02d%02d%02d%02d%02d", now->tm_year-100, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); return szbuf;}/*========================================================================================================================================================*/char* DB_MySQL::getDBTime(char *szbuf){ time_t tt; struct tm *now; time(&tt); now = localtime( &tt); sprintf( szbuf, "20%02d-%02d-%02d %02d:%02d:%02d", now->tm_year-100, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); return szbuf;}////////////////////////////////////////////////////////////////////////////////Name:DivideMsg//Function:divide message into pages if the message exceed the limit length of submit message//Parameters://Return: >0=>total records that is in search result// =0->/////////////////////////////////////////////////////////////////////////////int DB_MySQL::DivideMsg(char *szBuf){ unsigned int i,j,k,nCount; char buf[20] = {0}; char sPage[MAXPAGE][MAXBUF]; char rem[MAXBUF] = {0}; //initial the page counter nCount = 0; //begin to divide the msg into pages while (strlen(szBuf) >= m_nMaxLength*2){ k = 0; for (i = 0;i < m_nMaxLength*2;i++){ memset(buf,0,20*sizeof(char)); sprintf(buf,"%x",szBuf[i]); if (memcmp(buf,"ffff",4)==0) k++; if (i == (m_nMaxLength*2 - 1)){ if (fmod((double)k,2) == 0) j = i+1; else j = i; }//end if }//end for //the string we cut memset(sPage[nCount],0,MAXBUF*sizeof(char)); memcpy(sPage[nCount],szBuf,j); //the string that remained memset(rem,0,MAXBUF*sizeof(char)); for (i=0;i<=(strlen(szBuf)-j);i++) rem[i] = szBuf[j+i]; //copy the remain string to current string memset(szBuf,0,strlen(szBuf)*sizeof(char)); strcpy(szBuf,rem); //pages of the divide msg nCount++; }//end while //the remain msg is in limit of the msg if (strlen(szBuf)>0) strcpy(sPage[nCount],szBuf); //put the msg into the arrary set for (i = 0;i <= nCount;i++){ memset(m_sPageMsg[i],0,MAXBUF*sizeof(char)); sprintf(m_sPageMsg[i],"%d/%d) %s",i+1,nCount+1,sPage[i]); }//end for //return the size of the array return nCount;}/*===========================================================================------------------------------写数据库的MO_Deliver表--------------------------=============================================================================*/int DB_MySQL::WriteSMGP_Submit(SMGP_Submit *pSubmit){ sprintf(szSql, "INSERT INTO MT_Submit (nMsgType,nNeedReport,nPriority,sServerID,sFeeType,sFeeCode,sFixedFee,nMsgFormat,sValidTime,sAtTime,sReplyPath,sChargeTermId,sDestTermId,nMsgLength,sMsgContent) VALUES(%d,%d,%d,'%s','%s','%s','%s',%d,'%s','%s','%s','%s','%s',%d,'%s')", pSubmit->nMsgType, pSubmit->nNeedReport, pSubmit->nPriority, pSubmit->sServerId, pSubmit->sFeeType, pSubmit->sFeeCode, pSubmit->sFixedFee, pSubmit->nMsgFormat, pSubmit->sValidTime, pSubmit->sAtTime, pSubmit->sReplyPath, pSubmit->sChargeTermId, pSubmit->sDestTermId, pSubmit->nMsgLength, pSubmit->sMsgContent); if (mysql_query(&conn, szSql)) { cout <<"Can't insert data to MT_Submit table.\n"; mysql_close(&conn ); return 1; } return 0;}/*========================================================================================================================================================*/int DB_MySQL::WriteSMGP_MO(SMGP_Deliver *pDeliver){ //printf("time=%s",pDeliver->sRecvTime); sprintf(szSql, "INSERT INTO SMGP_MO (smsTime,descId,srcId,data) VALUES('%s','%s','%s','%s')", pDeliver->sRecvTime, pDeliver->sDestTermId, pDeliver->sSrcTermId, pDeliver->Deliver_Msg.sMsgContent); if (mysql_query(&conn, szSql)) { cout<<"Can't insert data to SMGP_MO table"<<endl; mysql_close(&conn ); return 1; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -