C++操做mySql數據庫

1.首先下載配置mySql(可參考:http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html)html

2.配置VC環境mysql

(1)將libmysql.lib文件拷貝到安裝路徑VC/bin下ios

(2)將libmysql.dll文件拷貝到windows/system32下(沒有這一步,運行提示會報「沒法找到libmysql.dll」的錯誤)sql

(3)配置屬性C/C++ —> 常規 —> 附件包含目錄添加D:\mySQL\mysql_5.6.24_winx64\lib數據庫

(4)配置屬性連接器 —> 常規 —> 附件包含目錄添加D:\mySQL\mysql_5.6.24_winx64\libwindows

(5)配置屬性連接器 —> 輸入 —> 附加依賴性添加libmysql.lib數據結構

3.寫代碼fetch

代碼示例:spa

  1 #include <winsock.h>
  2 #include <iostream>
  3 #include <string>
  4 #include <mysql.h>
  5 using namespace std;
  6 
  7 int mainconnectMysql()
  8 {
  9     //必備的一個數據結構
 10     MYSQL mydata;
 11 
 12     //初始化數據庫
 13     if (0 == mysql_library_init(0, NULL, NULL))
 14     {
 15         cout << "mysql_library_init() succeed" << endl;
 16     }
 17     else
 18     {
 19         cout << "mysql_library_init() failed" << endl;
 20         return -1;
 21     }
 22 
 23     //初始化數據結構
 24     if (NULL != mysql_init(&mydata))
 25     {
 26         cout << "mysql_init() succeed" << endl;
 27     }
 28     else
 29     {
 30         cout << "mysql_init() failed" << endl;
 31         return -1;
 32     }
 33 
 34     //在鏈接數據庫以前,設置額外的鏈接選項
 35     //能夠設置的選項不少,這裏設置字符集,不然沒法處理中文
 36 //    if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk"))
 37     if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk"))
 38     {
 39         cout << "mysql_options() succeed" << endl;
 40     }
 41     else
 42     {
 43         cout << "mysql_options() failed" << endl;
 44         return -1;
 45     }
 46 
 47     //鏈接數據庫
 48     if (NULL != mysql_real_connect(&mydata, "127.0.0.1", "root", "110", "test", 3306, NULL, 0))  //"127.0.0.1" <=====> "localhost"
 49         //這裏的地址,用戶名,密碼,端口能夠根據本身本地的狀況更改
 50     {
 51         cout << "mysql_real_connect() succeed" << endl;
 52     }
 53     else
 54     {
 55         cout << "mysql_real_connect() failed" << endl;
 56         return -1;
 57     }
 58 
 59 
 60 
 61     //sql字符串
 62     string sqlstr;
 63 
 64     //建立一個表
 65 //    sqlstr = "CREATE TABLE IF NOT EXISTS Heros";
 66 //    sqlstr += "(ID char(1) not null,NAME varchar(10) not null,SEX char(1) not null,AGE int(2) not null);";
 67 
 68     //====================
 69         sqlstr = "CREATE TABLE IF NOT EXISTS Heros";
 70         sqlstr += "(";
 71         sqlstr +="id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique User ID',";
 72         sqlstr +="name VARCHAR(100) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL COMMENT 'Name Of User',";  //gb2312_chinese_CI : 只支持簡體中文
 73         sqlstr += "sex Char(1) NOT NULL,";
 74         sqlstr +="age INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'The Summation Of Using Time'";
 75         sqlstr += ");";
 76 
 77     if (0 == mysql_query(&mydata, sqlstr.c_str()))
 78     {
 79         cout << "mysql_query() create table succeed" << endl;
 80     }
 81     else
 82     {
 83         cout << "mysql_query() create table failed" << endl;
 84         mysql_close(&mydata);
 85         return -1;
 86     }
 87 
 88     //===========================
 89     //mysql_query(&mydata, "SET NAMES latin1");
 90     //===========================
 91 
 92     //向表中插入數據
 93 //    sqlstr = "INSERT INTO Heros(ID,NAME,SEX,AGE) VALUES('1','宋江','m',45),('2','盧俊義','f',40), ('3', '吳用', 'm', 39),('4','公孫勝','m',36),('5', '關勝', 'm', 38), ('6', '林沖', 'f', 34);";
 94     sqlstr = "INSERT INTO Heros(NAME,SEX,AGE) VALUES('宋江','m',45),('盧俊義','f',40), ('吳用', 'm', 39),('公孫勝','m',36),('關勝', 'm', 38), ('林沖', 'f', 34);";
 95 //    sqlstr = "INSERT INTO Heros(NAME,SEX) VALUES('宋江','m'),('盧俊義','f'), ('吳用', 'm'),('公孫勝','m'),('關勝', 'm'), ('林沖', 'f');";
 96 
 97     
 98     if (0 == mysql_query(&mydata, sqlstr.c_str()))
 99     {
100         cout << "mysql_query() insert data succeed" << endl;
101     }
102     else
103     {
104         cout << "mysql_query() insert data failed" << endl;
105         mysql_close(&mydata);
106         return -1;
107     }
108 
109     //顯示剛纔插入的數據
110     sqlstr = "SELECT id,name,sex,age FROM Heros";
111     MYSQL_RES *result = NULL;
112     if (0 == mysql_query(&mydata, sqlstr.c_str()))
113     {
114         cout << "mysql_query() select data succeed" << endl;
115 
116         //一次性取得數據集
117         result = mysql_store_result(&mydata);
118         //取得並打印行數
119         int rowcount = mysql_num_rows(result);
120         cout << "row count: " << rowcount << endl;
121 
122         //取得並打印各字段的名稱
123         unsigned int fieldcount = mysql_num_fields(result);
124         MYSQL_FIELD *field = NULL;
125         for (unsigned int i = 0; i < fieldcount; i++)
126         {
127             field = mysql_fetch_field_direct(result, i);
128             cout << field->name << "\t\t";
129         }
130         cout << endl;
131 
132         //打印各行
133         MYSQL_ROW row = NULL;
134         row = mysql_fetch_row(result);
135         while (NULL != row)
136         {
137             for (int i = 0; i < fieldcount; i++)
138             {
139                 cout << row[i] << "\t\t";
140             }
141             cout << endl;
142             row = mysql_fetch_row(result);
143         }
144 
145     }
146     else
147     {
148         cout << "mysql_query() select data failed" << endl;
149         mysql_close(&mydata);
150         return -1;
151     }
152 
153 //    //刪除剛纔建的表
154 //    sqlstr = "DROP TABLE user_info";
155 //    if (0 == mysql_query(&mydata, sqlstr.c_str()))
156 //    {
157 //        cout << "mysql_query() drop table succeed" << endl;
158 //    }
159 //    else
160 //    {
161 //        cout << "mysql_query() drop table failed" << endl;
162 //        mysql_close(&mydata);
163 //        return -1;
164 //    }
165 //    mysql_free_result(result);
166 //    mysql_close(&mydata);
167 //    mysql_server_end();
168 
169     system("pause");
170     return 0;
171 }
相關文章
相關標籤/搜索