个人代码

#include "stdafx.h" #include "mysql.h" #include "RainfallList.h" #include "gmxcfg.h" CRainfallList::CRainfallList() : m_pList(0), m_nClicks(0) { } CRainfallList::~CRainfallList() { if (m_pList) delete [] m_pList; } /* ** Get Rain List. */ // // | |/////////////////////////////////| // |-----------------------------|---------------------------------| // | | | // start_time - 24*3600 start_time start_time + hours*3600 int CRainfallList::Find(char *site_id, long start_time, int hours) { char szSQL[256]; char msg[512], host[64]; MYSQL *myData; MYSQL_RES *res; MYSQL_ROW row; m_nClicks = 0; m_startClick.tm = 0; m_startClick.contJmp = 0; GetServerIP(host); if ((myData = mysql_init((MYSQL*) 0)) && mysql_real_connect( myData, host, "rain", "rain", NULL, MYSQL_PORT, NULL, 0 )) { if ( mysql_select_db( myData, "raindb") < 0 ) { ::MessageBox(0, "Can't select the database: raindb!", "GMX", 0) ; mysql_close( myData ) ; return -1; } } else { wsprintf(msg, "Can't connect to the mysql server on:/nhost:%s/nport %d !/n", host, MYSQL_PORT); ::MessageBox(0, msg, "GMX", 0); mysql_close( myData ) ; return -1; } wsprintf(szSQL, "select jump_time, jump from site_data where site_id='%s' and jump_time >= %d and jump_time <= %d order by jump_time, jump", site_id, start_time - 24*3600, start_time + hours * 3600); if (!mysql_query (myData, szSQL)) { res = mysql_store_result(myData) ; m_nClicks = res ? (long) mysql_num_rows(res) : 0; if (m_nClicks > 0) { if (m_pList) delete [] m_pList; m_pList = new RFCLICK[m_nClicks]; } int i = 0; while (row = mysql_fetch_row(res)) { if (atol(row[0]) < start_time) /* 取起始日前一天的最后一个跳次 */ { m_startClick.tm = atoi(row[0]); m_startClick.contJmp = atoi(row[1]); } else { m_pList[i].tm = atol(row[0]); m_pList[i].contJmp = atol(row[1]); ++i; } } mysql_free_result( res ) ; m_nClicks = i; } else { int e = mysql_errno(myData); const char *err = mysql_error(myData); wsprintf(msg, "Couldn't execute %s on the server !/n", szSQL) ; ::MessageBox(0, msg, "GMX", 0); } mysql_close (myData) ; return m_nClicks; } /** * 同一小时 sec 秒最大雨量 */ int CRainfallList::getMM_MaxInSameHour(time_t start_time, int hours, MAXMM *unit, int sec) { long h1, h2; int s, e; int delta; int N = m_nClicks; ASSERT (sec < 3600); ZeroMemory((char *)unit, hours*sizeof(MAXMM)); if (m_nClicks == 0) return 0; for (s = 0, e = 1; s < m_nClicks - 1 && e < N; ++s) { h1 = (m_pList[s].tm - start_time); if (h1 >= 0) h1 /= 3600; h2 = (m_pList[e].tm - start_time); if (h2 >= 0) h2 /= 3600; if (h1 >= 0 && h1 < hours && h2 >= 0 && h2 < hours) { while (e < N && (m_pList[e].tm - m_pList[s].tm) < sec && h1 == h2) { delta = m_pList[e].contJmp - m_pList[s].contJmp + 1; if (delta > unit[h1].max) { unit[h1].idx_end = e; unit[h1].idx_start = s; unit[h1].max = delta; unit[h1].start = m_pList[s].tm; unit[h1].end = m_pList[e].tm; } ++e; if (e < N) h2 = (long) difftime(m_pList[e].tm, start_time) / 3600; } // while } else ++e; } return 1; } // 计算每一个小时的连续雨量和时雨量 // Result in array, subscript is hour void CRainfallList::getMM_Hour(time_t start_time, int hours, int *start_mm, int *mm) { long diff; int h; int i; ZeroMemory ((char*)mm, sizeof(int)*hours); ZeroMemory ((char*)start_mm, sizeof(int)*hours); diff = m_startClick.tm - start_time; if (diff > -24*3600 && diff < 0) { for (h = 0; h < (diff + 24*3600+3599)/3600; h++) { start_mm[h] = m_startClick.contJmp; } } for (i = 0; i < m_nClicks; i++) { diff = m_pList[i].tm - start_time; ASSERT (diff >= 0 && diff < hours*3600); if (diff >= 0 && diff < hours*3600) { for (h = (diff + 3599)/3600; h < min( (diff+24*3600+3599)/3600, hours ); h++) { start_mm[h] = m_pList[i].contJmp; } mm[diff/3600]++; } } return; }

你可能感兴趣的:(VC/MFC,mysql,360,delete,server,database,include)