Skip to content

Commit b4a89e3

Browse files
Updates
1 parent f4f8e9d commit b4a89e3

File tree

6 files changed

+52
-13
lines changed

6 files changed

+52
-13
lines changed

2 - create table.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ int main (int argc, char *argv[])
2222

2323
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2424
fprintf(stderr, "Drop error, %s\n", PQerrorMessage(conn));
25-
2625
PQclear(res);
27-
PQfinish(conn);
28-
29-
exit(1);
26+
PQfinish(conn);
27+
exit(0);
3028
}
3129

3230
PQclear(res);
@@ -41,11 +39,9 @@ int main (int argc, char *argv[])
4139

4240
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
4341
fprintf(stderr, "Create error, %s\n", PQerrorMessage(conn));
44-
4542
PQclear(res);
46-
PQfinish(conn);
47-
48-
exit(1);
43+
PQfinish(conn);
44+
exit(0);
4945
}
5046

5147
PQclear(res);

3 - insert item.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ int main (int argc, char *argv[])
3737

3838
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
3939
fprintf(stderr, "Insert error, %s\n", PQerrorMessage(conn));
40-
4140
PQclear(res);
42-
PQfinish(conn);
43-
44-
exit(1);
41+
PQfinish(conn);
42+
exit(0);
4543
}
4644

45+
PQclear(res);
4746
printf("Item successfully inserted into the database.\n");
4847

4948
// Close connection.

4 - select all.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ int main (int argc, char *argv[])
1616
}
1717

1818
printf("Connected!\n");
19-
19+
20+
// Run SQL command.
2021
PGresult *res = PQexec(conn, "SELECT * FROM example");
2122

2223
if (PQresultStatus(res) != PGRES_TUPLES_OK) {

5 - delete.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ int main (int argc, char *argv[])
1717

1818
printf("Connected!\n");
1919

20+
// Delete data.
2021
PGresult *res = PQexec(conn, "DELETE FROM example");
2122

2223
if (PQresultStatus(res) != PGRES_COMMAND_OK) {

6 - update.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <libpq-fe.h>
5+
#include "settings.h"
6+
7+
int main (int argc, char *argv[])
8+
{
9+
// Connect to the database.
10+
PGconn *conn = PQconnectdb("host=" HOSTNAME " dbname=" DATABASE " user=" USERNAME " password=" PASSWORD);
11+
12+
if (PQstatus(conn) == CONNECTION_BAD) {
13+
fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
14+
PQfinish(conn);
15+
exit(0);
16+
}
17+
18+
printf("Connected!\n");
19+
20+
// Update data.
21+
PGresult *res = PQexec(conn, "UPDATE example SET name = 'New name' WHERE id = 1");
22+
23+
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
24+
fprintf(stderr, "Update error, %s\n", PQerrorMessage(conn));
25+
PQclear(res);
26+
PQfinish(conn);
27+
exit(0);
28+
}
29+
30+
printf("Items successfully updated.\n");
31+
printf("Total: %d\n", atoi(PQcmdTuples(res)));
32+
PQclear(res);
33+
34+
// Close connection.
35+
PQfinish(conn);
36+
printf("Disconnected!\n");
37+
38+
return 0;
39+
}
40+
41+

readme-pt.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Códigos:
9292
- <b>3 - insert item.c</b>: Insere registros em uma tabela.
9393
- <b>4 - select all.c</b>: Seleciona os registros de uma tabela.
9494
- <b>5 - delete.c</b>: Exclui registros.
95+
- <b>6 - update.c</b>: Atualiza registros.
9596

9697
<br>
9798

0 commit comments

Comments
 (0)