/* *介紹:MySQL的簡單封裝,支持流操作輸入輸出MySQL語句,然而並沒有什麼軟用,大二學生自娛自樂,有不足求指點 *作者:MrEO *日期:2016.3.26 */
頭文件 my_sql.h
1 #ifndef MY_SQL_H
2 #define MY_SQL_H
3
4 #include <mysql.h>
5 #include <iostream>
6 #include <string>
7 #include <iomanip>
8
9 class My_sql
10 {
11 public:
12 My_sql(); //初始化API、初始化結構、設置字符集、連接數據庫
13 ~My_sql(); //釋放內存、關閉服務器連接、結束服務器庫、結束API庫
14 unsigned row_count(); //返回集中的行數
15 unsigned field_count(); //返回集中的列數
16 friend std::ostream & operator <<(std::ostream &out, My_sql &sql);
17 //通過std::ostream輸出列名、結果集
18 friend std::istream & operator >>(std::istream &in, My_sql &sql);
19 //通過std::istream輸入執行SQL語句,並保存結果集地址至成員變量result
20 // friend My_sql & operator >>(const std::string &q, My_sql &sql);
21 //通過std::string輸入執行SQL語句 ,並保存結果集地址至成員變量result
22 friend My_sql & operator <<(My_sql &sql, const std::string &q);
23 //通過std::string輸入執行SQL語句 ,並保存結果集地址至成員變量result
24
25 protected:
26 bool query(const std::string &q); //執行SQL語句,保存完整的結果集地址至成員變量result
27 My_sql & display_field_name(std::ostream &out = std::cout, int setw_n = 16); //輸出列名
28 My_sql & display_result(std::ostream &out = std::cout, int setw_n = 16); //輸出結果集
29 MYSQL mysql; //對象
30 MYSQL_RES *result = NULL; //結果集
31 MYSQL_FIELD *field = NULL; //列
32 MYSQL_ROW row; //行
33 };
34
35 #endif
實現文件 my_sql.cpp
1 #include "my_sql.h"
2
3 My_sql::My_sql()
4 {
5 mysql_library_init( NULL, 0, 0 ); //初始化MySQL C API庫
6 mysql_init( &mysql ); //獲取或初始化MYSQL結構。
7 mysql_options( &mysql, MYSQL_SET_CHARSET_NAME, "utf8" ); //設置字符集
8 mysql_real_connect( &mysql, "localhost", "root", "password", "my_sql", 0, NULL, CLIENT_MULTI_STATEMENTS );
9 //連接數據庫
10 }
11 My_sql::~My_sql()
12 {
13 mysql_free_result( result ); //釋放結果集使用的內存
14 mysql_close( &mysql ); //關閉服務器連接
15 mysql_server_end(); //最終確定嵌入式服務器庫
16 mysql_library_end(); //最終確定MySQL C API庫
17 }
18 bool My_sql::query(const std::string &q)
19 {
20 result = NULL;
21 mysql_query( &mysql, q.c_str() ); //執行指定為“以Null終結的字符串”的SQL查詢。
22 result = mysql_store_result( &mysql ); //檢索完整的結果集至客戶端
23 if ( NULL == result ) {
24 std::cerr << "Query Error!";
25 return false;
26 }
27 return true;
28 }
29
30 unsigned My_sql::row_count()
31 {
32 return mysql_num_rows( result ); //返回集中的行數
33 }
34 unsigned My_sql::field_count()
35 {
36 return mysql_num_fields( result ); //返回集中的列數
37 }
38
39 My_sql & My_sql::display_field_name(std::ostream &out, int setw_n)
40 {
41 for ( unsigned int i = 0; i < field_count(); ++i ) {
42 out << std::setw(setw_n) << ( mysql_fetch_field_direct( result, i ) -> name );
43 //給定字段編號,返回表字段的類型。輸出列名
44 }
45 out << std::endl;
46 return *this;
47 }
48
49 My_sql & My_sql::display_result(std::ostream &out, int setw_n)
50 {
51 while( row = mysql_fetch_row( result ) ) //從結果集中獲取下一行
52 {
53 for(int i = 0; i < field_count(); ++i )
54 {
55 if ( NULL == row[i]) {
56 out << std::setw(setw_n) << "(NULL)";
57 }
58 else {
59 out << std::setw(setw_n) << row[i];
60 }
61 }
62 out << std::endl;
63 }
64 return *this;
65 }
66
67 std::ostream & operator <<(std::ostream &out, My_sql &sql)
68 {
69 sql.display_field_name(out).display_result(out);
70 //輸出列名和結果集
71
72 return out;
73 }
74
75 std::istream & operator >>(std::istream &in, My_sql &sql)
76 {
77 std::string q;
78 std::getline(in, q); //輸入整行
79 if ( !sql.query(q) ) {
80 exit(1);
81 }
82 return in;
83 }
84
85 //My_sql & operator >>(const std::string &q, My_sql &sql)
86 //{
87 // if ( !sql.query(q) ) {
88 // exit(1);
89 // }
90 // return sql;
91 //}
92
93 My_sql & operator <<(My_sql &sql, const std::string &q)
94 {
95 if ( !sql.query(q) ) {
96 exit(1);
97 }
98 return sql;
99 }
調用實例 main.cpp
1 #include <iostream>
2 #include "my_sql.h"
3
4 using std::cin;
5 using std::cout;
6
7 int main(int argc, char* argv[])
8 {
9 My_sql sql;
10 sql << "SELECT * FROM my_table";
11 cout << sql;
12 cin >> sql;
13 cout << sql;
14 return 0;
15 }