/
mysql_driver.c
93 lines (76 loc) · 2.11 KB
/
mysql_driver.c
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
/*
* This file is part of netactuator.
*
* netactuator is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* netactuator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with netactuator. If not, see <http://www.gnu.org/licenses/gpl.html>
*
*/
#include <mysql/mysql.h>
MYSQL *mysql_conn_global;
//int conectar (void)
MYSQL * conectar (void)
{
MYSQL *mysql_conn;
int i, flag_conectou=0;
mysql_conn = mysql_init(NULL);
if (mysql_conn == NULL)
{
printf("mysql_init() failed (probably out of memory)\n");
return NULL;
}
// mysql_conn, host, user, passwd, db
for(i=0; i<NUM_DB_TRY; i++)
{
//printf("tentando conectar %d\n", i);
if (mysql_real_connect(mysql_conn, db_host, db_user, db_password, db_name, 0, NULL, 0))
{
flag_conectou = 1;
break;
}
sleep(1);
} // for
if (!flag_conectou)
{
printf("Erro ao conectar ao banco: %s\n", mysql_error(mysql_conn));
mysql_close(mysql_conn);
return NULL;
}
else
{
// printf("Conectado...\n");
return mysql_conn;
}
}
//void desconectar (void)
void desconectar (MYSQL *mysql_conn)
{
mysql_close(mysql_conn);
// printf("Desconectado...\n");
}
// Executa um SQL e retorna o pointer dos valores encontrados
MYSQL_RES * sql(tipostring query, MYSQL *mysql_conn)
{
MYSQL_RES *res=NULL;
int t;
t = mysql_real_query(mysql_conn, query, (unsigned int) strlen(query));
if (t)
{
printf("Erro ao fazer a query: %s\n", mysql_error(mysql_conn));
return NULL;
}
else
{
res = mysql_use_result(mysql_conn);
return res;
}
}