Database.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. #include "pch.h"
  2. #include "Database.h"
  3. #define CHECKDB if (m_pConn == NULL || !m_bConnected) return FALSE
  4. char* CDatabase::pImage = NULL;
  5. CDatabase::CDatabase():m_pConn(NULL), m_pError(NULL), m_bConnected(FALSE)
  6. {
  7. m_pConn = mysql_init(NULL);
  8. }
  9. CDatabase::~CDatabase()
  10. {
  11. mysql_close(m_pConn);
  12. mysql_library_end();
  13. }
  14. BOOL CDatabase::SetBinaryField(std::string strCondition, void* pDataIn, int nDataLen)
  15. {
  16. CHECKDB;
  17. BOOL bResult = FALSE;
  18. MYSQL_STMT* stmt = NULL;
  19. stmt = mysql_stmt_init(m_pConn);
  20. if (!stmt)
  21. return FALSE;
  22. if (!mysql_stmt_prepare(stmt, strCondition.c_str(), strCondition.length()))
  23. {
  24. MYSQL_BIND bind;
  25. memset(&bind, 0, sizeof(MYSQL_BIND));
  26. bind.buffer_type = MYSQL_TYPE_BLOB;
  27. bind.buffer_length = nDataLen;
  28. bind.buffer = pDataIn;
  29. if (!mysql_stmt_bind_param(stmt, (MYSQL_BIND*)&bind))
  30. {
  31. if (!mysql_stmt_execute(stmt))
  32. {
  33. bResult = TRUE;
  34. }
  35. }
  36. }
  37. mysql_stmt_close(stmt);
  38. return bResult;
  39. }
  40. BOOL CDatabase::GetBinaryField(std::string strCondition, void** lpDataOut, unsigned long& nDataLen)
  41. {
  42. CHECKDB;
  43. BOOL bResult = FALSE;
  44. MYSQL_STMT* stmt = NULL;
  45. stmt = mysql_stmt_init(m_pConn);
  46. if (!stmt)
  47. return FALSE;
  48. MYSQL_RES* prepare_meta_result;
  49. if (!mysql_stmt_prepare(stmt, strCondition.c_str(), strCondition.length()))
  50. {
  51. if ( !mysql_stmt_execute(stmt) )
  52. {
  53. /* Fetch result set meta information */
  54. prepare_meta_result = mysql_stmt_result_metadata(stmt);
  55. if (prepare_meta_result)
  56. {
  57. /* Get total columns in the query */
  58. //int column_count = mysql_num_fields(prepare_meta_result);
  59. my_bool is_null;
  60. my_bool error;
  61. MYSQL_BIND bind[1];
  62. memset(bind, 0, sizeof(bind));
  63. bind[0].buffer_type = MYSQL_TYPE_BLOB;
  64. bind[0].buffer = 0;
  65. bind[0].buffer_length = 0;
  66. bind[0].is_null = &is_null;
  67. bind[0].length = &nDataLen; // 返回的缓存长度;
  68. bind[0].error = &error;
  69. /* Bind the result buffers */
  70. if (!mysql_stmt_bind_result(stmt, bind))
  71. {
  72. // 由于没有绑定缓冲区指针,第一次返回缓冲区大小;
  73. if (mysql_stmt_fetch(stmt))
  74. {
  75. if (nDataLen > 1)
  76. {
  77. *lpDataOut = new char[nDataLen];
  78. memset(*lpDataOut, 0, nDataLen);
  79. bind[0].buffer = *lpDataOut;
  80. bind[0].buffer_length = nDataLen;
  81. mysql_stmt_fetch_column(stmt, bind, 0, 0);
  82. bResult = TRUE;
  83. }
  84. }
  85. }
  86. /* Free the prepared result metadata */
  87. mysql_free_result(prepare_meta_result);
  88. }
  89. }
  90. }
  91. /* Close the statement */
  92. mysql_stmt_close(stmt);
  93. return bResult;
  94. }
  95. BOOL CDatabase::Init(std::string host, std::string user, std::string password, std::string db)
  96. {
  97. if (!m_pConn)
  98. return FALSE;
  99. m_pError = mysql_error(m_pConn);
  100. if (!mysql_real_connect(m_pConn, host.c_str(), user.c_str(), password.c_str(), db.c_str(), 0, NULL, 0)) {
  101. #ifdef _DEBUG
  102. TRACE1(_T("mysql_real_connect error=%s\n"), m_pError ? m_pError: "");
  103. #endif
  104. return FALSE;
  105. }
  106. return m_bConnected = TRUE;
  107. }
  108. BOOL CDatabase::ExcuteSQL(std::string sql)
  109. {
  110. if (m_pConn == NULL || !m_bConnected)
  111. return FALSE;
  112. if (0 != mysql_query(m_pConn, sql.c_str())) {
  113. #ifdef _DEBUG
  114. TRACE1(_T("ExcuteSQL error=%s\n"), m_pError ? m_pError : "");
  115. #endif
  116. return FALSE;
  117. }
  118. return TRUE;
  119. }
  120. BOOL CDatabase::InserProvider(const STProvider& provider)
  121. {
  122. CHECKDB;
  123. TCHAR szSql[MAX_PATH] = {0};
  124. _stprintf_s(szSql, INSER_PROVIDER, provider.name.c_str(), provider.note.c_str());
  125. if (0 != mysql_query(m_pConn, szSql)) {
  126. #ifdef _DEBUG
  127. TRACE1(_T("InserProvider error=%s\n"), m_pError ? m_pError : "");
  128. #endif
  129. return FALSE;
  130. }
  131. return TRUE;
  132. }
  133. BOOL CDatabase::InserProvider(std::string name, std::string note)
  134. {
  135. CHECKDB;
  136. TCHAR szSql[MAX_PATH] = { 0 };
  137. _stprintf_s(szSql, INSER_PROVIDER, name.c_str(), note.c_str());
  138. if (0 != mysql_query(m_pConn, szSql)) {
  139. #ifdef _DEBUG
  140. TRACE1(_T("InserProvider error=%s\n"), m_pError ? m_pError : "");
  141. #endif
  142. return FALSE;
  143. }
  144. return TRUE;
  145. }
  146. BOOL CDatabase::DeleteProvider(std::string name)
  147. {
  148. CHECKDB;
  149. TCHAR szSql[MAX_PATH] = { 0 };
  150. _stprintf_s(szSql, DEL_PROVIDER, name.c_str());
  151. if (0 != mysql_query(m_pConn, szSql)) {
  152. #ifdef _DEBUG
  153. TRACE1(_T("DeleteProvider error=%s\n"), m_pError ? m_pError : "");
  154. #endif
  155. return FALSE;
  156. }
  157. return TRUE;
  158. }
  159. BOOL CDatabase::UpdateProvider(std::string name, const STProvider& provider)
  160. {
  161. CHECKDB;
  162. TCHAR szSql[MAX_PATH] = { 0 };
  163. _stprintf_s(szSql, MOD_PROVIDER, provider.name.c_str(), provider.note.c_str(), name.c_str());
  164. if (0 != mysql_query(m_pConn, szSql)) {
  165. #ifdef _DEBUG
  166. TRACE1(_T("DeleteProvider error=%s\n"), m_pError ? m_pError : "");
  167. #endif
  168. return FALSE;
  169. }
  170. return TRUE;
  171. }
  172. BOOL CDatabase::QueryProvider(std::vector<STProvider>& vtProvider)
  173. {
  174. CHECKDB;
  175. if (0 != mysql_query(m_pConn, QUERY_PROVIDER)) {
  176. #ifdef _DEBUG
  177. TRACE1(_T("QueryProvider error=%s\n"), m_pError ? m_pError : "");
  178. #endif
  179. return FALSE;
  180. }
  181. // 获取表数据;
  182. MYSQL_RES* pData = mysql_store_result(m_pConn);
  183. if ( pData == NULL ) {
  184. #ifdef _DEBUG
  185. TRACE1(_T("QueryProvider error=%s\n"), m_pError ? m_pError : "");
  186. #endif
  187. return FALSE;
  188. }
  189. #ifdef _DEBUG
  190. // 统计表字段;
  191. unsigned int nLen = mysql_num_fields(pData);
  192. // 字段长度是否一致;
  193. if (nLen != 2) {
  194. mysql_free_result(pData);
  195. return FALSE;
  196. }
  197. // 打印出字段名称;
  198. TCHAR szLog[MAX_PATH] = {0};
  199. for ( int i = 0; i < nLen; i++ ) {
  200. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  201. OutputDebugString(szLog);
  202. }
  203. #endif
  204. // 遍历数据;
  205. MYSQL_ROW row;
  206. while ( (row = mysql_fetch_row(pData)) != NULL ) {
  207. STProvider provider;
  208. provider.name = row[0];
  209. provider.note = row[1];
  210. vtProvider.push_back(provider);
  211. }
  212. // 释放内存;
  213. mysql_free_result(pData);
  214. return TRUE;
  215. }
  216. BOOL CDatabase::InserSoc(const STSOC& soc)
  217. {
  218. CHECKDB;
  219. #if 0
  220. TCHAR szSql[MAX_PATH] = { 0 };
  221. _stprintf_s(szSql, INSER_SOC, soc.name.c_str(), soc.provider.c_str(), soc.note.c_str());
  222. if (0 != mysql_query(m_pConn, szSql)) {
  223. #ifdef _DEBUG
  224. TRACE1(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
  225. #endif
  226. return FALSE;
  227. }
  228. return TRUE;
  229. #else
  230. MYSQL_STMT* stmt;
  231. MYSQL_BIND bind[3];
  232. TCHAR szQuery[MAX_PATH] = { 0 };
  233. strcpy_s(szQuery, _T("INSERT INTO soc(name, provider, note) VALUES(?,?,?);"));
  234. //初始化stmt
  235. stmt = mysql_stmt_init(m_pConn);
  236. //预处理语句
  237. mysql_stmt_prepare(stmt, szQuery, strlen(szQuery));
  238. //初始化参数
  239. bind[0].buffer_type = MYSQL_TYPE_VARCHAR;
  240. bind[0].buffer = (void*)soc.name.c_str();
  241. bind[0].buffer_length = soc.name.size();
  242. bind[0].is_null = 0;
  243. bind[1].buffer_type = MYSQL_TYPE_VARCHAR;
  244. bind[1].buffer = (void*)soc.provider.c_str();
  245. bind[1].buffer_length = soc.provider.size();
  246. bind[1].is_null = 0;
  247. bind[2].buffer_type = MYSQL_TYPE_VARCHAR;
  248. bind[2].buffer = (void*)soc.note.c_str();
  249. bind[2].buffer_length = soc.note.size();
  250. bind[2].is_null = 0;
  251. //绑定参数123
  252. mysql_stmt_bind_param(stmt, bind);
  253. //执行预处理mysql语句
  254. if (0 != mysql_stmt_execute(stmt))
  255. {
  256. #ifdef _DEBUG
  257. TRACE1(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
  258. #endif
  259. return FALSE;
  260. }
  261. return TRUE;
  262. #endif
  263. }
  264. BOOL CDatabase::InserSoc(std::string name, std::string provider, std::string note)
  265. {
  266. CHECKDB;
  267. #if 0
  268. TCHAR szSql[MAX_PATH] = { 0 };
  269. _stprintf_s(szSql, INSER_SOC, name.c_str(), provider.c_str(), note.c_str());
  270. if (0 != mysql_query(m_pConn, szSql)) {
  271. #ifdef _DEBUG
  272. TRACE1(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
  273. #endif
  274. return FALSE;
  275. }
  276. return TRUE;
  277. #else
  278. //https://dev.mysql.com/doc/c-api/5.6/en/mysql-stmt-execute.html
  279. MYSQL_STMT* stmt;
  280. MYSQL_BIND bind[3];
  281. TCHAR szQuery[MAX_PATH] = { 0 };
  282. strcpy_s(szQuery, _T("INSERT INTO soc(name, provider, note) VALUES(?,?,?);"));
  283. //初始化stmt
  284. stmt = mysql_stmt_init(m_pConn);
  285. if (!stmt)
  286. return FALSE;
  287. //预处理语句
  288. if (mysql_stmt_prepare(stmt, szQuery, strlen(szQuery)))
  289. return FALSE;
  290. // 字段数量是否匹配;
  291. int param_count = mysql_stmt_param_count(stmt);
  292. if (param_count != 3)
  293. return FALSE;
  294. //初始化参数
  295. memset(bind, 0, sizeof(bind));
  296. bind[0].buffer_type = MYSQL_TYPE_VARCHAR;
  297. bind[0].buffer = (void*)name.c_str();
  298. bind[0].buffer_length = name.size();
  299. bind[0].is_null = 0;
  300. bind[1].buffer_type = MYSQL_TYPE_VARCHAR;
  301. bind[1].buffer = (void*)provider.c_str();
  302. bind[1].buffer_length = provider.size();
  303. bind[1].is_null = 0;
  304. bind[2].buffer_type = MYSQL_TYPE_VARCHAR;
  305. bind[2].buffer = (void*)note.c_str();
  306. bind[2].buffer_length = note.size();
  307. bind[2].is_null = 0;
  308. //绑定参数123
  309. if (0 != mysql_stmt_bind_param(stmt, bind))
  310. {
  311. #ifdef _DEBUG
  312. TRACE1(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
  313. #endif
  314. return FALSE;
  315. }
  316. //执行预处理mysql语句
  317. if (0 != mysql_stmt_execute(stmt))
  318. {
  319. #ifdef _DEBUG
  320. TRACE1(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
  321. #endif
  322. return FALSE;
  323. }
  324. // 影响的行数;
  325. int affected_rows = mysql_stmt_affected_rows(stmt);
  326. if (affected_rows != 1)
  327. return FALSE;
  328. // Close the statement;
  329. if (mysql_stmt_close(stmt))
  330. return FALSE;
  331. return TRUE;
  332. #endif
  333. }
  334. BOOL CDatabase::DeleteSoc(std::string name)
  335. {
  336. CHECKDB;
  337. TCHAR szSql[MAX_PATH] = { 0 };
  338. _stprintf_s(szSql, DEL_SOC, name.c_str());
  339. if (0 != mysql_query(m_pConn, szSql)) {
  340. #ifdef _DEBUG
  341. TRACE1(_T("DeleteSoc error=%s\n"), m_pError ? m_pError : "");
  342. #endif
  343. return FALSE;
  344. }
  345. return TRUE;
  346. }
  347. BOOL CDatabase::UpdateSoc(std::string name, const STSOC& soc)
  348. {
  349. CHECKDB;
  350. TCHAR szSql[MAX_PATH] = { 0 };
  351. _stprintf_s(szSql, MOD_SOC, soc.name.c_str(), soc.provider.c_str(), soc.note.c_str(), name.c_str());
  352. if (0 != mysql_query(m_pConn, szSql)) {
  353. #ifdef _DEBUG
  354. TRACE1(_T("UpdateSoc error=%s\n"), m_pError ? m_pError : "");
  355. #endif
  356. return FALSE;
  357. }
  358. return TRUE;
  359. }
  360. BOOL CDatabase::QuerySoc(std::vector<STSOC>& vtSoc)
  361. {
  362. CHECKDB;
  363. if (0 != mysql_query(m_pConn, QUERY_SOC)) {
  364. #ifdef _DEBUG
  365. TRACE1(_T("QuerySoc error=%s\n"), m_pError ? m_pError : "");
  366. #endif
  367. return FALSE;
  368. }
  369. // 获取表数据;
  370. MYSQL_RES* pData = mysql_store_result(m_pConn);
  371. if (pData == NULL) {
  372. #ifdef _DEBUG
  373. TRACE1(_T("QuerySoc error=%s\n"), m_pError ? m_pError : "");
  374. #endif
  375. return FALSE;
  376. }
  377. #ifdef _DEBUG
  378. // 统计表字段;
  379. unsigned int nLen = mysql_num_fields(pData);
  380. // 字段长度是否一致;
  381. if (nLen != 3) {
  382. mysql_free_result(pData);
  383. return FALSE;
  384. }
  385. // 打印出字段名称;
  386. TCHAR szLog[MAX_PATH] = { 0 };
  387. for (int i = 0; i < nLen; i++) {
  388. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  389. OutputDebugString(szLog);
  390. }
  391. #endif
  392. // 遍历数据;
  393. MYSQL_ROW row;
  394. while ((row = mysql_fetch_row(pData)) != NULL) {
  395. STSOC soc;
  396. soc.name = row[0];
  397. soc.provider = row[1];
  398. soc.note = row[2];
  399. vtSoc.push_back(soc);
  400. }
  401. // 释放内存;
  402. mysql_free_result(pData);
  403. return TRUE;
  404. }
  405. BOOL CDatabase::InsertBrand(const STBranch& brand)
  406. {
  407. CHECKDB;
  408. TCHAR szSql[MAX_PATH] = { 0 };
  409. _stprintf_s(szSql, INSER_BRAND, brand.name.c_str(), brand.note.c_str());
  410. if (0 != mysql_query(m_pConn, szSql)) {
  411. #ifdef _DEBUG
  412. TRACE1(_T("InsertBrand error=%s\n"), m_pError ? m_pError : "");
  413. #endif
  414. return FALSE;
  415. }
  416. return TRUE;
  417. }
  418. BOOL CDatabase::InsertBrand(std::string name, std::string note)
  419. {
  420. CHECKDB;
  421. TCHAR szSql[MAX_PATH] = { 0 };
  422. _stprintf_s(szSql, INSER_BRAND, name.c_str(), note.c_str());
  423. if (0 != mysql_query(m_pConn, szSql)) {
  424. #ifdef _DEBUG
  425. TRACE1(_T("InsertBrand error=%s\n"), m_pError ? m_pError : "");
  426. #endif
  427. return FALSE;
  428. }
  429. return TRUE;
  430. }
  431. BOOL CDatabase::DeleteBrand(std::string name)
  432. {
  433. CHECKDB;
  434. TCHAR szSql[MAX_PATH] = { 0 };
  435. _stprintf_s(szSql, DEL_BRAND, name.c_str());
  436. if (0 != mysql_query(m_pConn, szSql)) {
  437. #ifdef _DEBUG
  438. TRACE1(_T("DeleteBrand error=%s\n"), m_pError ? m_pError : "");
  439. #endif
  440. return FALSE;
  441. }
  442. return TRUE;
  443. }
  444. BOOL CDatabase::UpdateBrand(std::string name, const STBranch& brand)
  445. {
  446. CHECKDB;
  447. TCHAR szSql[MAX_PATH] = { 0 };
  448. _stprintf_s(szSql, MOD_BRAND, brand.name.c_str(), brand.note.c_str(), name.c_str());
  449. if (0 != mysql_query(m_pConn, szSql)) {
  450. #ifdef _DEBUG
  451. TRACE1(_T("UpdateBrand error=%s\n"), m_pError ? m_pError : "");
  452. #endif
  453. return FALSE;
  454. }
  455. return TRUE;
  456. }
  457. BOOL CDatabase::QueryBrand(std::vector<STBranch>& vtBrand)
  458. {
  459. CHECKDB;
  460. if (0 != mysql_query(m_pConn, QUERY_BRAND)) {
  461. #ifdef _DEBUG
  462. TRACE1(_T("QueryBrand error=%s\n"), m_pError ? m_pError : "");
  463. #endif
  464. return FALSE;
  465. }
  466. // 获取表数据;
  467. MYSQL_RES* pData = mysql_store_result(m_pConn);
  468. if (pData == NULL) {
  469. #ifdef _DEBUG
  470. TRACE1(_T("QueryBrand error=%s\n"), m_pError ? m_pError : "");
  471. #endif
  472. return FALSE;
  473. }
  474. #ifdef _DEBUG
  475. // 统计表字段;
  476. unsigned int nLen = mysql_num_fields(pData);
  477. // 字段长度是否一致;
  478. if (nLen != 2) {
  479. mysql_free_result(pData);
  480. return FALSE;
  481. }
  482. // 打印出字段名称;
  483. TCHAR szLog[MAX_PATH] = { 0 };
  484. for (int i = 0; i < nLen; i++) {
  485. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  486. OutputDebugString(szLog);
  487. }
  488. #endif
  489. // 遍历数据;
  490. MYSQL_ROW row;
  491. while ((row = mysql_fetch_row(pData)) != NULL) {
  492. STBranch brand;
  493. brand.name = row[0];
  494. brand.note = row[1];
  495. vtBrand.push_back(brand);
  496. }
  497. // 释放内存;
  498. mysql_free_result(pData);
  499. return TRUE;
  500. }
  501. BOOL CDatabase::ImportLogo(std::string name, std::string file)
  502. {
  503. if (!PathFileExists(file.c_str()))
  504. return FALSE;
  505. return 0;
  506. }
  507. BOOL CDatabase::ExportLogo(std::string name, std::string file)
  508. {
  509. return 0;
  510. }
  511. BOOL CDatabase::InsertQuarter(const STQuarter& quarter)
  512. {
  513. CHECKDB;
  514. TCHAR szSql[MAX_PATH] = { 0 };
  515. _stprintf_s(szSql, INSER_QUARTER, quarter.name.c_str(), quarter.scp.c_str(), quarter.note.c_str());
  516. if (0 != mysql_query(m_pConn, szSql)) {
  517. #ifdef _DEBUG
  518. TRACE1(_T("InsertQuarter error=%s\n"), m_pError ? m_pError : "");
  519. #endif
  520. return FALSE;
  521. }
  522. return TRUE;
  523. }
  524. BOOL CDatabase::InsertQuarter(std::string name, std::string scp, std::string note)
  525. {
  526. CHECKDB;
  527. TCHAR szSql[MAX_PATH] = { 0 };
  528. _stprintf_s(szSql, INSER_QUARTER, name.c_str(), scp.c_str(), note.c_str());
  529. if (0 != mysql_query(m_pConn, szSql)) {
  530. #ifdef _DEBUG
  531. TRACE1(_T("InsertQuarter error=%s\n"), m_pError ? m_pError : "");
  532. #endif
  533. return FALSE;
  534. }
  535. return TRUE;
  536. }
  537. BOOL CDatabase::DeleteQuarter(std::string name)
  538. {
  539. CHECKDB;
  540. TCHAR szSql[MAX_PATH] = { 0 };
  541. _stprintf_s(szSql, DEL_QUARTER, name.c_str());
  542. if (0 != mysql_query(m_pConn, szSql)) {
  543. #ifdef _DEBUG
  544. TRACE1(_T("DeleteQuarter error=%s\n"), m_pError ? m_pError : "");
  545. #endif
  546. return FALSE;
  547. }
  548. return TRUE;
  549. }
  550. BOOL CDatabase::UpdateQuarter(std::string name, const STQuarter& quarter)
  551. {
  552. CHECKDB;
  553. TCHAR szSql[MAX_PATH] = { 0 };
  554. _stprintf_s(szSql, MOD_QUARTER,quarter.name.c_str(), quarter.scp.c_str(), quarter.note.c_str(), name.c_str());
  555. if (0 != mysql_query(m_pConn, szSql)) {
  556. #ifdef _DEBUG
  557. TRACE1(_T("UpdateQuarter error=%s\n"), m_pError ? m_pError : "");
  558. #endif
  559. return FALSE;
  560. }
  561. return TRUE;
  562. }
  563. BOOL CDatabase::QueryQuarter(std::vector<STQuarter>& vtQuarter)
  564. {
  565. CHECKDB;
  566. if (0 != mysql_query(m_pConn, QUERY_QUARTER)) {
  567. #ifdef _DEBUG
  568. TRACE1(_T("QueryQuarter error=%s\n"), m_pError ? m_pError : "");
  569. #endif
  570. return FALSE;
  571. }
  572. // 获取表数据;
  573. MYSQL_RES* pData = mysql_store_result(m_pConn);
  574. if (pData == NULL) {
  575. #ifdef _DEBUG
  576. TRACE1(_T("QueryQuarter error=%s\n"), m_pError ? m_pError : "");
  577. #endif
  578. return FALSE;
  579. }
  580. #ifdef _DEBUG
  581. // 统计表字段;
  582. unsigned int nLen = mysql_num_fields(pData);
  583. // 字段长度是否一致;
  584. if (nLen != 3) {
  585. mysql_free_result(pData);
  586. return FALSE;
  587. }
  588. // 打印出字段名称;
  589. TCHAR szLog[MAX_PATH] = { 0 };
  590. for (int i = 0; i < nLen; i++) {
  591. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  592. OutputDebugString(szLog);
  593. }
  594. #endif
  595. // 遍历数据;
  596. MYSQL_ROW row;
  597. while ((row = mysql_fetch_row(pData)) != NULL) {
  598. STQuarter quarter;
  599. quarter.name = row[0];
  600. quarter.scp = row[1];
  601. quarter.note = row[2];
  602. vtQuarter.push_back(quarter);
  603. }
  604. // 释放内存;
  605. mysql_free_result(pData);
  606. return TRUE;
  607. }
  608. BOOL CDatabase::InsertServer(const STServer& Server)
  609. {
  610. CHECKDB;
  611. TCHAR szSql[MAX_PATH] = { 0 };
  612. _stprintf_s(szSql, INSER_SERVER, Server.name.c_str(), Server.ip.c_str(), Server.type.c_str(), Server.user.c_str(), Server.password.c_str(),Server.note.c_str());
  613. if (0 != mysql_query(m_pConn, szSql)) {
  614. #ifdef _DEBUG
  615. TRACE1(_T("InsertServer error=%s\n"), m_pError ? m_pError : "");
  616. #endif
  617. return FALSE;
  618. }
  619. return TRUE;
  620. }
  621. BOOL CDatabase::InsertServer(std::string name, std::string ip, std::string type, std::string user, std::string password, std::string note)
  622. {
  623. CHECKDB;
  624. TCHAR szSql[MAX_PATH] = { 0 };
  625. _stprintf_s(szSql, INSER_SERVER, name.c_str(), ip.c_str(), type.c_str(), user.c_str(), password.c_str(), note.c_str());
  626. if (0 != mysql_query(m_pConn, szSql)) {
  627. #ifdef _DEBUG
  628. TRACE1(_T("InsertServer error=%s\n"), m_pError ? m_pError : "");
  629. #endif
  630. return FALSE;
  631. }
  632. return TRUE;
  633. }
  634. BOOL CDatabase::DeleteServer(std::string name)
  635. {
  636. CHECKDB;
  637. TCHAR szSql[MAX_PATH] = { 0 };
  638. _stprintf_s(szSql, DEL_SERVER, name.c_str());
  639. if (0 != mysql_query(m_pConn, szSql)) {
  640. #ifdef _DEBUG
  641. TRACE1(_T("DeleteServer error=%s\n"), m_pError ? m_pError : "");
  642. #endif
  643. return FALSE;
  644. }
  645. return TRUE;
  646. }
  647. BOOL CDatabase::UpdateServer(std::string name, const STServer& Server)
  648. {
  649. CHECKDB;
  650. TCHAR szSql[MAX_PATH] = { 0 };
  651. _stprintf_s(szSql, MOD_SERVER, Server.name.c_str(), Server.ip.c_str(), Server.type.c_str(), Server.user.c_str(), Server.password.c_str(), Server.note.c_str(), name.c_str());
  652. if (0 != mysql_query(m_pConn, szSql)) {
  653. #ifdef _DEBUG
  654. TRACE1(_T("UpdateServer error=%s\n"), m_pError ? m_pError : "");
  655. #endif
  656. return FALSE;
  657. }
  658. return TRUE;
  659. }
  660. BOOL CDatabase::QueryServer(std::vector<STServer>& vtServer)
  661. {
  662. CHECKDB;
  663. if (0 != mysql_query(m_pConn, QUERY_SERVER)) {
  664. #ifdef _DEBUG
  665. TRACE1(_T("QueryServer error=%s\n"), m_pError ? m_pError : "");
  666. #endif
  667. return FALSE;
  668. }
  669. // 获取表数据;
  670. MYSQL_RES* pData = mysql_store_result(m_pConn);
  671. if (pData == NULL) {
  672. #ifdef _DEBUG
  673. TRACE1(_T("QueryServer error=%s\n"), m_pError ? m_pError : "");
  674. #endif
  675. return FALSE;
  676. }
  677. #ifdef _DEBUG
  678. // 统计表字段;
  679. unsigned int nLen = mysql_num_fields(pData);
  680. // 字段长度是否一致;
  681. if (nLen != 6) {
  682. mysql_free_result(pData);
  683. return FALSE;
  684. }
  685. // 打印出字段名称;
  686. TCHAR szLog[MAX_PATH] = { 0 };
  687. for (int i = 0; i < nLen; i++) {
  688. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  689. OutputDebugString(szLog);
  690. }
  691. #endif
  692. // 遍历数据;
  693. MYSQL_ROW row;
  694. while ((row = mysql_fetch_row(pData)) != NULL) {
  695. STServer server;
  696. server.name = row[0];
  697. server.ip = row[1];
  698. server.type = row[2];
  699. server.user = row[3];
  700. server.password = row[4];
  701. server.note = row[5];
  702. vtServer.push_back(server);
  703. }
  704. // 释放内存;
  705. mysql_free_result(pData);
  706. return TRUE;
  707. }
  708. // 示例;
  709. void test(MYSQL* mysql)
  710. {
  711. #define STRING_SIZE 50
  712. #define SELECT_SAMPLE "SELECT col1, col2, col3, col4 \
  713. FROM test_table"
  714. MYSQL_STMT* stmt;
  715. MYSQL_BIND bind[4];
  716. MYSQL_RES* prepare_meta_result;
  717. MYSQL_TIME ts;
  718. unsigned long length[4];
  719. int param_count, column_count, row_count;
  720. short small_data;
  721. int int_data;
  722. char str_data[STRING_SIZE];
  723. my_bool is_null[4];
  724. my_bool error[4];
  725. /* Prepare a SELECT query to fetch data from test_table */
  726. stmt = mysql_stmt_init(mysql);
  727. if (!stmt)
  728. {
  729. fprintf(stderr, " mysql_stmt_init(), out of memory\n");
  730. exit(0);
  731. }
  732. if (mysql_stmt_prepare(stmt, SELECT_SAMPLE, strlen(SELECT_SAMPLE)))
  733. {
  734. fprintf(stderr, " mysql_stmt_prepare(), SELECT failed\n");
  735. fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  736. exit(0);
  737. }
  738. fprintf(stdout, " prepare, SELECT successful\n");
  739. /* Get the parameter count from the statement */
  740. param_count = mysql_stmt_param_count(stmt);
  741. fprintf(stdout, " total parameters in SELECT: %d\n", param_count);
  742. if (param_count != 0) /* validate parameter count */
  743. {
  744. fprintf(stderr, " invalid parameter count returned by MySQL\n");
  745. exit(0);
  746. }
  747. /* Execute the SELECT query */
  748. if (mysql_stmt_execute(stmt))
  749. {
  750. fprintf(stderr, " mysql_stmt_execute(), failed\n");
  751. fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  752. exit(0);
  753. }
  754. /* Fetch result set meta information */
  755. prepare_meta_result = mysql_stmt_result_metadata(stmt);
  756. if (!prepare_meta_result)
  757. {
  758. fprintf(stderr,
  759. " mysql_stmt_result_metadata(), \
  760. returned no meta information\n");
  761. fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  762. exit(0);
  763. }
  764. /* Get total columns in the query */
  765. column_count = mysql_num_fields(prepare_meta_result);
  766. fprintf(stdout,
  767. " total columns in SELECT statement: %d\n",
  768. column_count);
  769. if (column_count != 4) /* validate column count */
  770. {
  771. fprintf(stderr, " invalid column count returned by MySQL\n");
  772. exit(0);
  773. }
  774. /* Bind the result buffers for all 4 columns before fetching them */
  775. memset(bind, 0, sizeof(bind));
  776. /* INTEGER COLUMN */
  777. bind[0].buffer_type = MYSQL_TYPE_LONG;
  778. bind[0].buffer = (char*)&int_data;
  779. bind[0].is_null = &is_null[0];
  780. bind[0].length = &length[0];
  781. bind[0].error = &error[0];
  782. /* STRING COLUMN */
  783. bind[1].buffer_type = MYSQL_TYPE_STRING;
  784. bind[1].buffer = (char*)str_data;
  785. bind[1].buffer_length = STRING_SIZE;
  786. bind[1].is_null = &is_null[1];
  787. bind[1].length = &length[1];
  788. bind[1].error = &error[1];
  789. /* SMALLINT COLUMN */
  790. bind[2].buffer_type = MYSQL_TYPE_SHORT;
  791. bind[2].buffer = (char*)&small_data;
  792. bind[2].is_null = &is_null[2];
  793. bind[2].length = &length[2];
  794. bind[2].error = &error[2];
  795. /* TIMESTAMP COLUMN */
  796. bind[3].buffer_type = MYSQL_TYPE_TIMESTAMP;
  797. bind[3].buffer = (char*)&ts;
  798. bind[3].is_null = &is_null[3];
  799. bind[3].length = &length[3];
  800. bind[3].error = &error[3];
  801. /* Bind the result buffers */
  802. if (mysql_stmt_bind_result(stmt, bind))
  803. {
  804. fprintf(stderr, " mysql_stmt_bind_result() failed\n");
  805. fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  806. exit(0);
  807. }
  808. /* Now buffer all results to client (optional step) */
  809. if (mysql_stmt_store_result(stmt))
  810. {
  811. fprintf(stderr, " mysql_stmt_store_result() failed\n");
  812. fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  813. exit(0);
  814. }
  815. /* Fetch all rows */
  816. row_count = 0;
  817. fprintf(stdout, "Fetching results ...\n");
  818. while (!mysql_stmt_fetch(stmt))
  819. {
  820. row_count++;
  821. fprintf(stdout, " row %d\n", row_count);
  822. /* column 1 */
  823. fprintf(stdout, " column1 (integer) : ");
  824. if (is_null[0])
  825. fprintf(stdout, " NULL\n");
  826. else
  827. fprintf(stdout, " %d(%ld)\n", int_data, length[0]);
  828. /* column 2 */
  829. fprintf(stdout, " column2 (string) : ");
  830. if (is_null[1])
  831. fprintf(stdout, " NULL\n");
  832. else
  833. fprintf(stdout, " %s(%ld)\n", str_data, length[1]);
  834. /* column 3 */
  835. fprintf(stdout, " column3 (smallint) : ");
  836. if (is_null[2])
  837. fprintf(stdout, " NULL\n");
  838. else
  839. fprintf(stdout, " %d(%ld)\n", small_data, length[2]);
  840. /* column 4 */
  841. fprintf(stdout, " column4 (timestamp): ");
  842. if (is_null[3])
  843. fprintf(stdout, " NULL\n");
  844. else
  845. fprintf(stdout, " %04d-%02d-%02d %02d:%02d:%02d (%ld)\n",
  846. ts.year, ts.month, ts.day,
  847. ts.hour, ts.minute, ts.second,
  848. length[3]);
  849. fprintf(stdout, "\n");
  850. }
  851. /* Validate rows fetched */
  852. fprintf(stdout, " total rows fetched: %d\n", row_count);
  853. if (row_count != 2)
  854. {
  855. fprintf(stderr, " MySQL failed to return all rows\n");
  856. exit(0);
  857. }
  858. /* Free the prepared result metadata */
  859. mysql_free_result(prepare_meta_result);
  860. /* Close the statement */
  861. if (mysql_stmt_close(stmt))
  862. {
  863. /* mysql_stmt_close() invalidates stmt, so call */
  864. /* mysql_error(mysql) rather than mysql_stmt_error(stmt) */
  865. fprintf(stderr, " failed while closing the statement\n");
  866. fprintf(stderr, " %s\n", mysql_error(mysql));
  867. exit(0);
  868. }
  869. }