use mysql as db store
authorEugene Crosser <crosser@average.org>
Sat, 19 Dec 2015 12:30:44 +0000 (15:30 +0300)
committerEugene Crosser <crosser@average.org>
Sat, 19 Dec 2015 12:30:44 +0000 (15:30 +0300)
linux/Makefile
linux/dbstore.c

index c602db7e4d427c5de648a0c744f64e1a22f7a145..6cde40061116bda6b06ad8169da4f5d38151c4b3 100644 (file)
@@ -1,7 +1,9 @@
-CFLAGS += -I$(BLUEZ) `pkg-config --cflags glib-2.0`
+CFLAGS += -I$(BLUEZ) `pkg-config --cflags glib-2.0` \
+       `pkg-config mariadb --cflags`
 LDLIBS = $(BLUEZ)/lib/.libs/libbluetooth-internal.a \
        $(BLUEZ)/src/.libs/libshared-glib.a \
 LDLIBS = $(BLUEZ)/lib/.libs/libbluetooth-internal.a \
        $(BLUEZ)/src/.libs/libshared-glib.a \
-       `pkg-config --libs glib-2.0`
+       `pkg-config --libs glib-2.0` \
+       `pkg-config mariadb --libs`
 
 all: pulsecounter
 
 
 all: pulsecounter
 
index edd443102fdcb1e4826c72b922a7b142e8cbe3e2..395654da09f681d35921206e0103d2decbf91093 100644 (file)
@@ -2,20 +2,39 @@
 #include <stdint.h>
 #include <time.h>
 
 #include <stdint.h>
 #include <time.h>
 
+#include <mysql/mysql.h>
+
 #include "dbstore.h"
 
 int dbstore(uint8_t which, uint32_t val)
 {
        time_t t;
        int i;
 #include "dbstore.h"
 
 int dbstore(uint8_t which, uint32_t val)
 {
        time_t t;
        int i;
+       MYSQL mysql;
+       int rc = 0;
        struct tm tm;
        char buf[64];
        struct tm tm;
        char buf[64];
-       char tstr[64];
+       char tstr[32];
        char *table = (which == 1) ? "coldcnt" : "hotcnt";
        char *table = (which == 1) ? "coldcnt" : "hotcnt";
+       char statement[64];
 
        t = time(NULL);
        (void)gmtime_r(&t, &tm);
        (void)strftime(tstr, sizeof(tstr), "%Y-%m-%d %H:%M:%S", &tm);
 
        t = time(NULL);
        (void)gmtime_r(&t, &tm);
        (void)strftime(tstr, sizeof(tstr), "%Y-%m-%d %H:%M:%S", &tm);
-       printf("insert into %s values (\"%s\",%u);\n", table, tstr, val);
-       return 0;
+       mysql_init(&mysql);
+       if(!mysql_real_connect(&mysql, NULL, "pulsecounter",
+                               "xxxxxxxxxxxxx", "watermeter", 0, NULL, 0)) {
+               fprintf(stderr, "mysql connect error: %s\n",
+                       mysql_error(&mysql));
+               return 1;
+       }
+       snprintf(statement, sizeof(statement),
+                "insert into %s values (\"%s\",%u);\n",
+                table, tstr, val);
+       rc = mysql_query(&mysql, statement);
+       if (rc)
+               fprintf(stderr, "mysql insert \"%s\" error: %s\n",
+                       statement, mysql_error(&mysql));
+       mysql_close(&mysql);
+       return rc;
 }
 }