BOSS 7.1.1
BESIII Offline Software System
Loading...
Searching...
No Matches
db_mysql.cpp
Go to the documentation of this file.
1#include "db_mysql.h"
2#include <unistd.h>
4 :m_Mysql(NULL)
5 ,m_Res(NULL)
6 ,m_bConnect(false)
7 ,m_bSelectDB(false)
8 ,m_bExecute(false)
9 ,m_bGetRow(false)
10 ,m_bInit(false)
11{
12 ;
13}
14db_mysql::db_mysql(string host,string user,string passwd,string db)
15 :m_Mysql(NULL)
16 ,m_Res(NULL)
17 ,m_bConnect(false)
18 ,m_bSelectDB(false)
19 ,m_bExecute(false)
20 ,m_bGetRow(false)
21{
22 Init(host,user,passwd,db);
23}
24
25void db_mysql::Init(string host,string user,string passwd,string db)
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}
37void db_mysql::SetQuery(string query)
38{
39 m_strQuery=query;
40}
42{
43 if(!m_bConnect)
44 {
45 my_bool b = 0;
46 if(!(m_Mysql=mysql_init(NULL)))
47 {
48 cout<<"Init mysql error!!"<<endl;
49 return false;
50 }
51// mysql_options(m_Mysql,MYSQL_REPORT_DATA_TRUNCATION,&b);
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}
79bool db_mysql::SelectDB(string db)
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}
95
96bool db_mysql::Execute(string query)
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 {
114 Free_Result();
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//zhaohs
122 Field_num=mysql_field_count(m_Mysql);
123 if(Field_num==0){
124 m_result=false;
125 }
126 else {m_result=true;}
127//zhaohs
128 m_Res = mysql_store_result(m_Mysql);
129 m_bExecute = true;
130 return true;
131}
132
133bool db_mysql::GetRow(my_ulonglong row)
134{
135 if(!m_bExecute)
136 {
137 cout<<"You must execute query before get row!"<<endl;
138 return false;
139 }
140 if(row!=NEXT_ROW)
141 {
142 if(row>=mysql_num_rows(m_Res)||row<0)
143 {
144// cout<<"Select row error! Row number must be set in range!"<<endl;
145 return false;
146 }
147 mysql_data_seek(m_Res,row);
148 }
149 m_Row=mysql_fetch_row(m_Res);
150 if(m_Row==NULL)
151 {
152 cout<<"fetch_row error!"<<endl;
153 return false;
154 }
155
156 m_bGetRow = true;
157 return true;
158}
159
160string db_mysql::GetField(unsigned int n)
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}
171//// gaizao
172/*string db_mysql::GetField(string name)
173{
174
175 if(!m_bGetRow)
176 {cout<<"You must get a row before getfield value!"<<endl;
177 return "";
178 }
179 if(m_Row[name]!=NULL){ return (string)m_Row[name];}
180 else return "";
181}*/
182///// myself---zhaohs
183string db_mysql::GetField_name(unsigned int n)
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// num_fields = mysql_num_fields(result);
195// m_field = mysql_fetch_fields(m_Res);
196 // std::cout<<m_field[n].name<<std::endl;
197// return m_field[n].name;
198
199
200}
201
202////
203unsigned long db_mysql::Num_Rows(void)
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}
212
214{
215 if(m_bExecute)
216 {
217 mysql_free_result(m_Res);
218 m_bExecute = false;
219 m_bGetRow = false;
220 }
221}
222
224{
225 if(m_bConnect)
226 {
227 Free_Result();
228 mysql_close(m_Mysql);
229 m_bConnect = false;
230 }
231}
232
234{
235 Free_Result();
236 Close();
237}
238
const Int_t n
#define NULL
void Free_Result(void)
Definition db_mysql.cpp:213
bool SelectDB(string db)
Definition db_mysql.cpp:79
bool GetRow(my_ulonglong row=NEXT_ROW)
Definition db_mysql.cpp:133
string GetField_name(unsigned int n)
Definition db_mysql.cpp:183
unsigned long Num_Rows(void)
Definition db_mysql.cpp:203
void SetQuery(string query)
Definition db_mysql.cpp:37
void Close(void)
Definition db_mysql.cpp:223
void Init(string host, string user, string passwd, string db="")
Definition db_mysql.cpp:25
bool Connect(void)
Definition db_mysql.cpp:41
bool Execute(string query="")
Definition db_mysql.cpp:96
unsigned int Field_num
Definition db_mysql.h:36
string GetField(unsigned int n)
Definition db_mysql.cpp:160
#define NEXT_ROW
Definition db_mysql.h:8
const double b
Definition slope.cxx:9