Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "/usr/include/postgresql/libpq-fe.h"
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- int main()
- {
- char * conntr = "host='83.168.204.143' dbname='travelfast' user='postgres' password='negergreger'"; /* specify the login settings */
- char query_string[256]; /* Holds constructed SQL query */
- char modify_string[256]; /* Holds constructed SQL query */
- char modify_first_string[256]; /* Holds constructed SQL query */
- int i; /* Holds the for loop variable */
- int pid; /* Holds the pid */
- PGconn *conn; /* Holds database connection */
- PGresult *res; /* Holds query result */
- PGresult *mod; /* Holds query result */
- PGresult *work; /* Holds query result */
- // Create the daemon
- if(fork()) return 0;
- pid = fork();
- if(pid)
- {
- return 0;
- }
- /* DB connection */
- conn = PQconnectdb(conntr);
- if (PQstatus(conn) == CONNECTION_BAD) /* did the connection fail? */
- {
- fprintf(stderr, "Connection to database failed.\n");
- fprintf(stderr, "%s", PQerrorMessage(conn));
- exit(1); /* Then exit program */
- }
- /* loop while the PID is running */
- while(1)
- {
- /* === The SQL Query === */
- /* Modify first, "assign these DB-lines to me" */
- sprintf(modify_first_string, "UPDATE foo set server_id = '1' WHERE number IN (SELECT number from foo where working_on = '0' and server_id = '0')");
- mod = PQexec(conn, modify_first_string);
- if (PQcmdTuples(mod) != 0) /* if any row were modified, proceed with the program*/
- {
- PQclear(mod);
- /* Select those values */
- sprintf(query_string, "SELECT * FROM foo WHERE server_id = '1' AND working_on = '0'"); /* Create an SELECT query */
- res = PQexec(conn, query_string); /* send the query */
- if (PQresultStatus(res) != PGRES_TUPLES_OK) /* did the query fail? */
- {
- fprintf(stderr, "SELECT query failed.\n");
- PQclear(res);
- PQfinish(conn);
- exit(1);
- }
- if (PQntuples(res) > 0)
- {
- for (i = 0; i < PQntuples(res); i++) /* loop through all rows returned */
- sprintf(modify_string, "UPDATE foo SET working_on = '1' WHERE server_id = '1' AND number = '%s'", PQgetvalue(res, i, 0));
- PQclear(res); /* free res for the select-query earlier */
- work = PQexec(conn, modify_string);
- PQclear(work); /* free result for the modify string */
- }
- else
- {
- sleep(1);
- }
- }
- else
- {
- printf("sleep 1sek\n");
- sleep(1);
- }
- /* End of the while-loop */
- }
- /* Close the connection */
- PQfinish(conn); /* disconnect from the database */
- /* Exit the program */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement