summaryrefslogtreecommitdiffstats
path: root/db_demo/reset.c
diff options
context:
space:
mode:
authorWilrik de Loose <wilrik@wilrik.nl>2008-05-06 12:35:55 (GMT)
committerWilrik de Loose <wilrik@wilrik.nl>2008-05-06 12:35:55 (GMT)
commitcadaee395e4161b559ab345d175c8b32c4950d19 (patch)
tree0de65c289d4b028d2d0fe442bb0ca0481f71deff /db_demo/reset.c
parentf56c1962f0aba7c5ad6364b443f92d72b4b8228c (diff)
download2iv55-cadaee395e4161b559ab345d175c8b32c4950d19.zip
2iv55-cadaee395e4161b559ab345d175c8b32c4950d19.tar.gz
2iv55-cadaee395e4161b559ab345d175c8b32c4950d19.tar.bz2
db_demo files
Diffstat (limited to 'db_demo/reset.c')
-rw-r--r--db_demo/reset.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/db_demo/reset.c b/db_demo/reset.c
new file mode 100644
index 0000000..6fd92f7
--- /dev/null
+++ b/db_demo/reset.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <time.h>
+
+#include "libpq-fe.h"
+#include "reset.h"
+
+int main(int argc, char *argv[]) {
+ int i;
+ int options;
+ int day;
+ int month;
+ int year;
+ PGconn *conn;
+ char SQL_query[512];
+ PGresult *res;
+
+ day = month = year = 0;
+
+ conn = PQsetdbLogin(PGHOST, PGPORT, PGOPTIONS, PGTTY, DBNAME, LOGIN, PWD);
+
+ if(CONNECTION_BAD == PQstatus(conn)) {
+ fprintf(stderr, "Connection to database '%s' failed.\n %s", DBNAME, PQerrorMessage(conn));
+ PQfinish(conn);
+ return 1;
+ }
+
+ if(0 < day) {
+ for(i = 0; i <= day; i++) {
+ sprintf(SQL_query, "UPDATE counter_day SET \"%0.2d\" = 0;", i);
+ res = PQexec(conn, SQL_query);
+
+ if((NULL == res) || (PGRES_COMMAND_OK != PQresultStatus(res))) {
+ printf("Unable to execute query: %s\n %s\n", SQL_query, PQerrorMessage(conn));
+ PQclear(res);
+ PQfinish(conn);
+ return 1;
+ }
+ PQclear(res);
+ }
+ }
+ if(0 < month) {
+ for(i = 1; i <= month; i++) {
+ sprintf(SQL_query, "UPDATE counter_month SET \"%0.2d\" = 0;", i);
+ res = PQexec(conn, SQL_query);
+
+ if((NULL == res) || (PGRES_COMMAND_OK != PQresultStatus(res))) {
+ printf("Unable to execute query: %s\n %s\n", SQL_query, PQerrorMessage(conn));
+ PQclear(res);
+ PQfinish(conn);
+ return 1;
+ }
+ PQclear(res);
+ }
+ }
+ if(1 == year) {
+ sprintf(SQL_query, "INSERT INTO counter_year VALUES (counter_year.year +1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);");
+ res = PQexec(conn, SQL_query);
+
+ if((NULL == res) || (PGRES_COMMAND_OK != PQresultStatus(res))) {
+ printf("Unable to execute query: %s\n %s\n", SQL_query, PQerrorMessage(conn));
+ PQclear(res);
+ PQfinish(conn);
+ return 1;
+ }
+ PQclear(res);
+ }
+ PQfinish(conn);
+
+ return 0;
+}