7 #include <mysql/mysql.h>
11 static char *host = NULL;
12 static char *user = NULL;
13 static char *pass = NULL;
14 static char *dbnm = "watermeter";
16 int dbconfig(char *conffile)
18 FILE *fp = fopen(conffile, "r");
24 while (fgets(buf, sizeof(buf), fp)) {
27 e = buf + strlen(buf) - 1;
35 for (k = buf; k < e && isspace(k); k++) /*nothing*/ ;
37 for (v = k; v < e && !isspace(v)
38 && *v != ':' && *v != '='; v++) /*nothing*/ ;
39 if (v < e && (*v == ':' || *v == '=')) v++;
40 for (; v < e && (isspace(v) || *v == ':' || *v == '=')
47 if (!strcmp(k, "host")) host = strdup(v);
48 else if (!strcmp(k, "user")) user = strdup(v);
49 else if (!strcmp(k, "password")) pass = strdup(v);
50 else if (!strcmp(k, "database")) dbnm = strdup(v);
60 int dbstore(uint8_t which, uint32_t val)
69 char *table = (which == 1) ? "coldcnt" : "hotcnt";
73 (void)gmtime_r(&t, &tm);
74 (void)strftime(tstr, sizeof(tstr), "%Y-%m-%d %H:%M:%S", &tm);
76 if(!mysql_real_connect(&mysql, host, user, pass, dbnm, 0, NULL, 0)) {
77 fprintf(stderr, "mysql connect error: %s\n",
81 snprintf(statement, sizeof(statement),
82 "insert into %s values (\"%s\",%u);\n",
84 rc = mysql_query(&mysql, statement);
86 fprintf(stderr, "mysql insert \"%s\" error: %s\n",
87 statement, mysql_error(&mysql));