9 #include <mysql/mysql.h>
13 static char *host = NULL;
14 static char *user = NULL;
15 static char *pass = NULL;
16 static char *dbnm = "watermeter";
18 int dbconfig(char *conffile)
20 FILE *fp = fopen(conffile, "r");
26 while (fgets(buf, sizeof(buf), fp)) {
29 e = buf + strlen(buf) - 1;
37 for (k = buf; k < e && isspace(k); k++) /*nothing*/ ;
39 for (v = k; v < e && !isspace(v)
40 && *v != ':' && *v != '='; v++) /*nothing*/ ;
41 if (v < e && (*v == ':' || *v == '=')) v++;
42 for (; v < e && (isspace(v) || *v == ':' || *v == '=')
49 if (!strcmp(k, "host")) host = strdup(v);
50 else if (!strcmp(k, "user")) user = strdup(v);
51 else if (!strcmp(k, "password")) pass = strdup(v);
52 else if (!strcmp(k, "database")) dbnm = strdup(v);
62 int dbstore(uint8_t which, uint32_t val)
71 char *table = (which == 1) ? "coldcnt" : "hotcnt";
75 (void)gmtime_r(&t, &tm);
76 (void)strftime(tstr, sizeof(tstr), "%Y-%m-%d %H:%M:%S", &tm);
78 if(!mysql_real_connect(&mysql, host, user, pass, dbnm, 0, NULL, 0)) {
79 g_warning("mysql connect error: %s\n", mysql_error(&mysql));
82 snprintf(statement, sizeof(statement),
83 "insert into %s values (\"%s\",%u);\n",
85 rc = mysql_query(&mysql, statement);
87 g_warning("mysql insert \"%s\" error: %s\n",
88 statement, mysql_error(&mysql));