/
mysql.cpp
207 lines (183 loc) · 4.66 KB
/
mysql.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "mysql.h"
Mysql *sistemas;//Declaramos la varible global sistemas
int Mysql::Conectar(){
if((sock=mysql_init(0))){
return 1;
}
else{
return 0;
}
}
void Mysql::Desconectar(){
mysql_close(sock);
}
int Mysql::ReConectar(){
if(!mysql_ping(sock)){
return 1;
}
else{
mysql_close(sock);
return 0;
}
}
int Mysql::Conexion_db(){
if(mysql_real_connect(sock,servidor.fn_str(),usuario.fn_str(), password.fn_str(), base_de_datos.fn_str(), puerto=MYSQL_PORT, NULL, CLIENT_MULTI_STATEMENTS)){
return 1;
}
else{
mysql_close(sock);
return 0;
}
}
void Mysql::Configurar_conexion(wxString host,wxString user, wxString pass, wxString db,unsigned puerto){
servidor=host;
usuario=user;
password=pass;
base_de_datos=db;
puerto=puerto;
}
int Mysql::Seleccionar_db(wxString db){
if(mysql_select_db(sock, db.fn_str())){
//Error al seleccionar la DB
return 0;
}
else{
return 1;
}
}
int Mysql::Obtener_tablas(wxChoice tablas[]){
if((resultado = mysql_list_tables(sock,NULL))){
tablas->Clear();//Limpiamos todos los item del choice para agrgar nuevos
c = (int) mysql_num_fields(resultado);
while((fila = mysql_fetch_row(resultado))){
for(int j = 0 ; j < c ; j++){
(fila[j]==NULL) ? tablas->Append(wxT("NULL")) : tablas->Append(wxString::FromAscii(fila[j]));
}
}
mysql_free_result(resultado); // Liberar el resultado de la consulta
return 1;
}
else{
return 0;
}
}
int Mysql::Consulta(char *consulta){
if(!mysql_query(sock,consulta)){
return 1;
}
else{
mysql_close(sock);
return 0;
}
}
int Mysql::Agragar_valor_fila_columnas(wxGrid *cuadro){
if((resultado = mysql_store_result(sock))){
c = (int) mysql_num_fields(resultado);
f = (int) mysql_num_rows(resultado);
for(int k=0; k<f;k++){
fila = mysql_fetch_row(resultado);
for(int j = 0 ; j < c ; j++){
cuadro->SetCellValue(k,j,wxString::FromAscii(fila[j]));
}
}
return 1;
}
else{
mysql_close(sock);
return 0;
}
}
void Mysql::Conexion_exito(bool YESoNO){
existe_db_conectada=YESoNO; //agragamos true si se conecto una db o false si no se conecto
}
bool Mysql::Verificar_si_existe_conexion(){
//Comprueba si se ha conectado con la db
if(existe_db_conectada){
return 1;
}
else{
return 0;
}
}
wxString Mysql::Errores(){
return wxString::FromAscii(mysql_error(sock));
}
int Mysql::Obtener_nombre_campos_y_valores(wxGrid *cuadro,wxString consulta){
if(!mysql_query(sock,consulta.fn_str())){
if((resultado = mysql_store_result(sock))) {
// Obtener el número de registros seleccionados:
f = (int) mysql_num_rows(resultado);
// Obtener el número de columnsa por fila:
c = (int) mysql_num_fields(resultado);
// Información sobre columnas usando mysql_fetch_field:
if(c!=cuadro->GetNumberCols()){
cuadro->AppendCols(c);//agregamos el numero de columnas x resultado
cuadro->AppendRows(f);//agregamos el numero de filas x resultado
for(int l = 0; l < c; l++) {
columna = mysql_fetch_field(resultado);
cuadro->SetColLabelValue( l,wxString::FromAscii(columna->name));
cuadro->SetRowLabelValue(l,wxString::Format(wxT("%i"),l+1));
}
for(int k=0; k<f;k++){
fila = mysql_fetch_row(resultado);
for(int j = 0 ; j < c ; j++){
cuadro->SetCellValue(k,j,wxString::FromAscii(fila[j]));
}
}
}
}
else{
return 0;
}
return 1;
}
else{
mysql_close(sock);
return 0;
}
}
int Mysql::Liberar_consulta(){
mysql_free_result(resultado); // Liberar el resultado de la consulta
return 1;
}
void Mysql::Agregar_si_recargo(bool YESoNO){
recargo_tablas=YESoNO;
}
int Mysql::Verificar_si_recargo_tablas(){
if(recargo_tablas){
return 1;
}
else{
return 0;
}
}
void Mysql::Guardar_valor_viejo(wxString valor){
valor_viejo=valor;
}
wxString Mysql::Recuperar_valor_viejo(){
return valor_viejo;
}
int Mysql::Obtener_dbs(wxChoice db[]){
if((resultado = mysql_list_dbs(sock,NULL))){
db->Clear();//Limpiamos todos los item del choice para agrgar nuevos
c = (int) mysql_num_fields(resultado);
while((fila = mysql_fetch_row(resultado))){
for(int j = 0 ; j < c ; j++){
(fila[j]==NULL) ? db->Append(wxT("NULL")) : db->Append(wxString::FromAscii(fila[j]));
}
}
mysql_free_result(resultado); // Liberar el resultado de la consulta
return 1;
}
else{
return 0;
}
}
bool Mysql::Cambiar_usuario(wxString new_usuario,wxString new_password,wxString new_db){
if(mysql_change_user(sock,new_usuario.fn_str(),new_password.fn_str(),new_db.fn_str())){
return false;//No tuvo exito
}
else{
return true;//Tuvo exito la consulta
}
}