BOSS 7.0.4
BESIII Offline Software System
Loading...
Searching...
No Matches
db_mysql.cpp
Go to the documentation of this file.
1#include "db_mysql.h"
2
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 mysql_close(m_Mysql);
56 return false;
57 }
58 }
59 else
60 {
61 cout<<"You are reconnecting!"<<endl;
62 return false;
63 }
64 m_bConnect = true;
65 return true;
66}
67bool db_mysql::SelectDB(string db)
68{
69 if(!m_bConnect)
70 {
71 cout<<"You must connect before select db!";
72 return false;
73 }
74 if(mysql_select_db(m_Mysql,db.c_str()))
75 {
76 cout<<"Select database error!"<<endl;
77 return false;
78 }
79 m_strDB = db;
80 m_bSelectDB = true;
81 return true;
82}
83
84bool db_mysql::Execute(string query)
85{
86 if((!m_bConnect)||(!m_bSelectDB))
87 {
88 cout<<"You executing query before connection or select db!"<<endl;
89 return false;
90 }
91 if(!query.empty())
92 {
93 m_strQuery=query;
94 }
95 if(m_strQuery.empty())
96 {
97 cout<<"Your haven't set query string!"<<endl;
98 return false;
99 }
100 else
101 {
102 Free_Result();
103 if(mysql_real_query(m_Mysql,m_strQuery.c_str(),m_strQuery.size()))
104 {
105 cout<<"Execute query error!"<<endl;
106 return false;
107 }
108 }
109//zhaohs
110 Field_num=mysql_field_count(m_Mysql);
111 if(Field_num==0){
112 m_result=false;
113 }
114 else {m_result=true;}
115//zhaohs
116 m_Res = mysql_store_result(m_Mysql);
117 m_bExecute = true;
118 return true;
119}
120
121bool db_mysql::GetRow(my_ulonglong row)
122{
123 if(!m_bExecute)
124 {
125 cout<<"You must execute query before get row!"<<endl;
126 return false;
127 }
128 if(row!=NEXT_ROW)
129 {
130 if(row>=mysql_num_rows(m_Res)||row<0)
131 {
132// cout<<"Select row error! Row number must be set in range!"<<endl;
133 return false;
134 }
135 mysql_data_seek(m_Res,row);
136 }
137 m_Row=mysql_fetch_row(m_Res);
138 if(m_Row==NULL)
139 {
140 cout<<"fetch_row error!"<<endl;
141 return false;
142 }
143
144 m_bGetRow = true;
145 return true;
146}
147
148string db_mysql::GetField(unsigned int n)
149{
150
151 if(!m_bGetRow)
152 {
153 cout<<"You must get a row before getfield value!"<<endl;
154 return "";
155 }
156 if(m_Row[n]!=NULL){ return (string)m_Row[n];}
157 else return "";
158}
159//// gaizao
160/*string db_mysql::GetField(string name)
161{
162
163 if(!m_bGetRow)
164 {cout<<"You must get a row before getfield value!"<<endl;
165 return "";
166 }
167 if(m_Row[name]!=NULL){ return (string)m_Row[name];}
168 else return "";
169}*/
170///// myself---zhaohs
171string db_mysql::GetField_name(unsigned int n)
172{
173 if(!m_result)
174 {
175 cout<<"error:no select!"<<endl;
176 return "";
177 }
178 m_field = mysql_fetch_field_direct(m_Res, n);
179 return string(m_field->name);
180
181
182// num_fields = mysql_num_fields(result);
183// m_field = mysql_fetch_fields(m_Res);
184 // std::cout<<m_field[n].name<<std::endl;
185// return m_field[n].name;
186
187
188}
189
190////
191unsigned long db_mysql::Num_Rows(void)
192{
193 if(!m_bExecute)
194 {
195 cout<<"You are get number of rows before execute!"<<endl;
196 return 0;
197 }
198 return mysql_num_rows(m_Res);
199}
200
202{
203 if(m_bExecute)
204 {
205 mysql_free_result(m_Res);
206 m_bExecute = false;
207 m_bGetRow = false;
208 }
209}
210
212{
213 if(m_bConnect)
214 {
215 Free_Result();
216 mysql_close(m_Mysql);
217 m_bConnect = false;
218 }
219}
220
222{
223 Free_Result();
224 Close();
225}
226
const Int_t n
void Free_Result(void)
Definition: db_mysql.cpp:201
bool SelectDB(string db)
Definition: db_mysql.cpp:67
bool GetRow(my_ulonglong row=NEXT_ROW)
Definition: db_mysql.cpp:121
string GetField_name(unsigned int n)
Definition: db_mysql.cpp:171
unsigned long Num_Rows(void)
Definition: db_mysql.cpp:191
void SetQuery(string query)
Definition: db_mysql.cpp:37
void Close(void)
Definition: db_mysql.cpp:211
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:84
unsigned int Field_num
Definition: db_mysql.h:36
db_mysql()
Definition: db_mysql.cpp:3
string GetField(unsigned int n)
Definition: db_mysql.cpp:148
#define NEXT_ROW
Definition: db_mysql.h:8