-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5 - delete.c
40 lines (29 loc) · 912 Bytes
/
5 - delete.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
#include "settings.h"
int main (int argc, char *argv[])
{
// Connect to the database.
PGconn *conn = PQconnectdb("host=" HOSTNAME " dbname=" DATABASE " user=" USERNAME " password=" PASSWORD);
if (PQstatus(conn) == CONNECTION_BAD) {
fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
PQfinish(conn);
exit(0);
}
printf("Connected!\n");
// Delete data.
PGresult *res = PQexec(conn, "DELETE FROM example");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "Delete error, %s\n", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
exit(0);
}
PQclear(res);
printf("Records deleted successfully.\n");
// Close connection.
PQfinish(conn);
printf("Disconnected!\n");
return 0;
}