From: Eugene Crosser Date: Sat, 19 Dec 2015 12:30:44 +0000 (+0300) Subject: use mysql as db store X-Git-Url: http://www.average.org/gitweb/?p=pulsecounter.git;a=commitdiff_plain;h=896a89d59b527f5099b38ea58aebeb6ad674ae13;ds=sidebyside use mysql as db store --- diff --git a/linux/Makefile b/linux/Makefile index c602db7..6cde400 100644 --- a/linux/Makefile +++ b/linux/Makefile @@ -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 \ - `pkg-config --libs glib-2.0` + `pkg-config --libs glib-2.0` \ + `pkg-config mariadb --libs` all: pulsecounter diff --git a/linux/dbstore.c b/linux/dbstore.c index edd4431..395654d 100644 --- a/linux/dbstore.c +++ b/linux/dbstore.c @@ -2,20 +2,39 @@ #include #include +#include + #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]; - char tstr[64]; + char tstr[32]; 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); - 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; }