#include <db_mysql.h>
Definition at line 9 of file db_mysql.h.
◆ db_mysql() [1/2]
Definition at line 3 of file db_mysql.cpp.
6 ,m_bConnect(false)
7 ,m_bSelectDB(false)
8 ,m_bExecute(false)
9 ,m_bGetRow(false)
10 ,m_bInit(false)
11{
12 ;
13}
◆ db_mysql() [2/2]
db_mysql::db_mysql |
( |
string | host, |
|
|
string | user, |
|
|
string | passwd, |
|
|
string | db = "" ) |
Definition at line 14 of file db_mysql.cpp.
17 ,m_bConnect(false)
18 ,m_bSelectDB(false)
19 ,m_bExecute(false)
20 ,m_bGetRow(false)
21{
22 Init(host,user,passwd,db);
23}
void Init(string host, string user, string passwd, string db="")
◆ ~db_mysql()
◆ Close()
void db_mysql::Close |
( |
void | | ) |
|
Definition at line 223 of file db_mysql.cpp.
224{
225 if(m_bConnect)
226 {
228 mysql_close(m_Mysql);
229 m_bConnect = false;
230 }
231}
Referenced by ~db_mysql().
◆ Connect()
bool db_mysql::Connect |
( |
void | | ) |
|
Definition at line 41 of file db_mysql.cpp.
42{
43 if(!m_bConnect)
44 {
46 if(!(m_Mysql=mysql_init(
NULL)))
47 {
48 cout<<"Init mysql error!!"<<endl;
49 return false;
50 }
51
52 if(!mysql_real_connect(m_Mysql,m_strHost.c_str(),m_strUser.c_str(),m_strPasswd.c_str(),m_strDB.c_str(),0,
NULL,0))
53 {
54 cout<<"Connect error!!"<<endl;
55 for(int i=0;i<10;i++)
56 {
57 cout<<"Reconnect ..."<<endl;
58 sleep(1);
59 if(mysql_real_connect(m_Mysql,m_strHost.c_str(),m_strUser.c_str(),m_strPasswd.c_str(),m_strDB.c_str(),0,
NULL,0))
60 {
61 cout<<"Reconnect success"<<endl;
62 m_bConnect = true;
63 return true;
64 }
65 cout<<"Reconnect fail"<<endl;
66 }
67 mysql_close(m_Mysql);
68 return false;
69 }
70 }
71 else
72 {
73 cout<<"You are reconnecting!"<<endl;
74 return false;
75 }
76 m_bConnect = true;
77 return true;
78}
◆ Execute()
bool db_mysql::Execute |
( |
string | query = "" | ) |
|
Definition at line 96 of file db_mysql.cpp.
97{
98 if((!m_bConnect)||(!m_bSelectDB))
99 {
100 cout<<"You executing query before connection or select db!"<<endl;
101 return false;
102 }
103 if(!query.empty())
104 {
105 m_strQuery=query;
106 }
107 if(m_strQuery.empty())
108 {
109 cout<<"Your haven't set query string!"<<endl;
110 return false;
111 }
112 else
113 {
115 if(mysql_real_query(m_Mysql,m_strQuery.c_str(),m_strQuery.size()))
116 {
117 cout<<"Execute query error!"<<endl;
118 return false;
119 }
120 }
121
124 m_result=false;
125 }
126 else {m_result=true;}
127
128 m_Res = mysql_store_result(m_Mysql);
129 m_bExecute = true;
130 return true;
131}
◆ Free_Result()
void db_mysql::Free_Result |
( |
void | | ) |
|
Definition at line 213 of file db_mysql.cpp.
214{
215 if(m_bExecute)
216 {
217 mysql_free_result(m_Res);
218 m_bExecute = false;
219 m_bGetRow = false;
220 }
221}
Referenced by Close(), Execute(), and ~db_mysql().
◆ GetField()
string db_mysql::GetField |
( |
unsigned int | n | ) |
|
Definition at line 160 of file db_mysql.cpp.
161{
162
163 if(!m_bGetRow)
164 {
165 cout<<"You must get a row before getfield value!"<<endl;
166 return "";
167 }
168 if(m_Row[
n]!=
NULL){
return (
string)m_Row[
n];}
169 else return "";
170}
◆ GetField_name()
string db_mysql::GetField_name |
( |
unsigned int | n | ) |
|
Definition at line 183 of file db_mysql.cpp.
184{
185 if(!m_result)
186 {
187 cout<<"error:no select!"<<endl;
188 return "";
189 }
190 m_field = mysql_fetch_field_direct(m_Res,
n);
191 return string(m_field->name);
192
193
194
195
196
197
198
199
200}
◆ GetRow()
Definition at line 133 of file db_mysql.cpp.
134{
135 if(!m_bExecute)
136 {
137 cout<<"You must execute query before get row!"<<endl;
138 return false;
139 }
141 {
142 if(row>=mysql_num_rows(m_Res)||row<0)
143 {
144
145 return false;
146 }
147 mysql_data_seek(m_Res,row);
148 }
149 m_Row=mysql_fetch_row(m_Res);
151 {
152 cout<<"fetch_row error!"<<endl;
153 return false;
154 }
155
156 m_bGetRow = true;
157 return true;
158}
◆ Init()
void db_mysql::Init |
( |
string | host, |
|
|
string | user, |
|
|
string | passwd, |
|
|
string | db = "" ) |
Definition at line 25 of file db_mysql.cpp.
26{
27 m_strHost = host;
28 m_strUser = user;
29 m_strPasswd = passwd;
30 m_strDB = db;
31 m_bInit = true;
32 if(!db.empty())
33 {
34 m_bSelectDB = true;
35 }
36}
Referenced by db_mysql().
◆ Num_Rows()
unsigned long db_mysql::Num_Rows |
( |
void | | ) |
|
Definition at line 203 of file db_mysql.cpp.
204{
205 if(!m_bExecute)
206 {
207 cout<<"You are get number of rows before execute!"<<endl;
208 return 0;
209 }
210 return mysql_num_rows(m_Res);
211}
◆ SelectDB()
bool db_mysql::SelectDB |
( |
string | db | ) |
|
Definition at line 79 of file db_mysql.cpp.
80{
81 if(!m_bConnect)
82 {
83 cout<<"You must connect before select db!";
84 return false;
85 }
86 if(mysql_select_db(m_Mysql,db.c_str()))
87 {
88 cout<<"Select database error!"<<endl;
89 return false;
90 }
91 m_strDB = db;
92 m_bSelectDB = true;
93 return true;
94}
◆ SetQuery()
void db_mysql::SetQuery |
( |
string | query | ) |
|
Definition at line 37 of file db_mysql.cpp.
38{
39 m_strQuery=query;
40}
◆ Field_num
unsigned int db_mysql::Field_num |
The documentation for this class was generated from the following files: