Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "wet.h"
- #include <stdio.h>
- #include <string.h>
- #include <libpq-fe.h>
- PGconn *conn;
- void checkCmd(PGresult* res) {
- if(!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stdout, "Error executing command: %s\n",
- PQresultErrorMessage(res));
- PQclear(res);
- return;
- }
- }
- void checkConn(PGconn* conn) {
- if (!conn || PQstatus(conn) == CONNECTION_BAD) {
- fprintf(stdout, "Connection to server failed: %s\n",
- PQerrorMessage(conn));
- PQfinish(conn);
- exit(1);
- }
- }
- int recreate (char* tname) {
- char cmd[400];
- PGresult *res;
- sprintf(cmd, "DROP TABLE %s",tname);
- res = PQexec(conn, cmd);
- checkCmd(res);
- sprintf(cmd, "CREATE TABLE %s AS SELECT * FROM course_%s",tname,tname);
- res = PQexec(conn, cmd);
- checkCmd(res);
- }
- int main ()
- {
- char cmd[400];
- char connect_param[200];
- PGresult *res;
- sprintf(connect_param,
- "host=pgsql.cs.technion.ac.il dbname=%s user=%s password=%s",
- USERNAME, USERNAME, PASSWORD);
- conn = PQconnectdb(connect_param);
- checkConn(conn);
- recreate("accepted");
- recreate("admin");
- recreate("article");
- recreate("books");
- recreate("borrowed");
- recreate("customers");
- recreate("editor");
- recreate("link");
- recreate("ordered");
- recreate("users");
- recreate("version");
- recreate("viewed");
- printf("Database recreated.\n");
- PQfinish(conn);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement