source: branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_sql.c @ 613

Last change on this file since 613 was 603, checked in by david, 10 years ago
  • sql status management
File size: 1.7 KB
RevLine 
[603]1#include <string.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <mysql.h>
5
6MYSQL *con;
7char * host;
8char *user;
9char *passwd;
10char *bdd;
11int port;
12
13
14void init_sql(const char* h,const char *u, const char *pas, const char * b,int p){
15    host = strdup(h);
16    user = strdup(u);
17    passwd = strdup(pas);
18    bdd = strdup(b);
19    port=p;
20}
21
22
23int init_connection(){
24    con = mysql_init(NULL);
25    if (con == NULL){
26        /* erreur allocation et initialisation mysql */
27        return -1;
28    }
29    if (mysql_real_connect(con,host,user,passwd,bdd,port,NULL,0) == NULL){
30        fprintf(stderr, "%s\n", mysql_error(con));
31        mysql_close(con);
32        return -2;
33    }
34    return 0;
35}
36
37
38char * get_uuid(){
39    init_connection();
40    char * query = "Select uuid();";
41    if (mysql_query(con, query) != 0){
42        fprintf(stderr,"%s\n", mysql_error(con));
43        return NULL;
44    }
45    MYSQL_RES *result = mysql_store_result(con);
46    if (result == NULL){
47        fprintf(stderr,"get_uuid: no uuid\n");
48        return NULL;
49    }
50    int num_fields = mysql_num_fields(result);
51    if (num_fields != 1)
52        return NULL;
53    MYSQL_ROW row = mysql_fetch_row(result);
54    char * tmp = strdup(row[0]);
55    mysql_free_result(result);
56    mysql_close(con);
57    return tmp;
58 }
59
60int add_status(const char * uuid) {
61    init_connection();
62    const char * query = "insert into status (uuid,status,created_time) values ('%s','queue',now())";
63    char * query_f = (char*) malloc(strlen(query) + strlen(uuid) + 1);
64    sprintf(query_f,query,uuid);
65    if (mysql_query(con, query_f) != 0){
66        fprintf(stderr, "%s\n", mysql_error(con));
67        free(query_f);
68        return -1;
69    }
70    free(query_f);
71    mysql_close(con);
72    return 0;
73}
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png