请选择 进入手机版 | 继续访问电脑版
查看: 466|回复: 0

[运维管理] 简单查询MYSQL

686

主题

686

帖子

2071

积分

猿er

Rank: 1

积分
2071
发表于 2016-8-14 17:48:15
mingw-cpp mysql
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <locale>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include "mysql-5.7.12-win32/mysql.h"
  9. using namespace std;
  10. /*
  11. test_table001
  12. -----------------------------------------
  13. char_id char_name char_password
  14. ------------------------------------------
  15. 10000 char_root 123456
  16. 10001 looyer ac0004
  17. */
  18. int main(int argc, char *argv[])
  19. {
  20. MYSQL mysql;
  21. if(0 == mysql_init(&mysql)) {
  22. cerr << "init mysql handle error!" << endl;
  23. }else {
  24. cout << "init mysql success!" << endl;
  25. }
  26. const char *szHost = "localhost";
  27. const char *szUser = "root";
  28. const char *szPort = "3306";
  29. const char *szPassword = "123456";
  30. const char *szDBName = "test_first";
  31. const char *szCharSet = "GBK";
  32. const char *szSQLTest = "SELECT char_id, char_name, char_password FROM test_table001";
  33. if(0 == mysql_real_connect(&mysql, szHost, szUser, szPassword, szDBName, 3306, 0, 0)) {
  34. cerr << "connect db error!" << endl;
  35. }else {
  36. cout << "db connect success!" << endl;
  37. }
  38. {
  39. MYSQL_ROW sqlRow;
  40. MYSQL_RES *psqlRes = 0;
  41. if(0 != mysql_query(&mysql, szSQLTest)) {
  42. cerr << "sql statement error!" << endl;
  43. }
  44. psqlRes = mysql_store_result(&mysql);
  45. if(0 == psqlRes)
  46. cerr << "query error!" << endl;
  47. cout << "***********************" << endl;
  48. cout << "row-count: " << mysql_num_rows(psqlRes) << " column-count: " << mysql_num_fields(psqlRes) << endl;
  49. //查询列的属性
  50. MYSQL_FIELD *pField = 0;
  51. for(int i = 0; i < mysql_num_fields(psqlRes); i++) {
  52. pField = mysql_fetch_field_direct(psqlRes, i);
  53. cout << "column" << i << "-name: " << pField->name << " ";
  54. }
  55. cout << endl;
  56. while(sqlRow = mysql_fetch_row(psqlRes)) {
  57. for(int i = 0; i < 3; i++) {
  58. cout << "***" << sqlRow[i] << endl;
  59. }
  60. }
  61. mysql_free_result(psqlRes);
  62. }
  63. mysql_close(&mysql);
  64. system("pause");
  65. return 0;
  66. }
复制代码
  1. ####################
  2. SRC = Test_mysql
  3. INC = -I ../External/include -I ../External/include/mysql-5.7.12-win32
  4. LIB = ../External/lib-mingw4.8.1/
  5. $(SRC): $(SRC).o
  6. g++ -g -o $(SRC) $(SRC).o $(LIB)libmysql.lib -lkernel32 -lgdi32 -luser32
  7. $(SRC).o: $(SRC).cpp
  8. g++ -g -c $(SRC).cpp $(INC)
复制代码
  1. make -f makefile_mysql.txt
  2. cmd
复制代码


回复

使用道具 举报