Database.cpp 25 KB

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