Skip to content

Commit 41dc7c0

Browse files
Updates
1 parent 01e0240 commit 41dc7c0

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

2 - create table.c

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

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

20+
// Delete table if exists.
21+
PGresult *res = PQexec(conn, "DROP TABLE IF EXISTS example");
22+
23+
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
24+
fprintf(stderr, "Drop error, %s\n", PQerrorMessage(conn));
25+
26+
PQclear(res);
27+
PQfinish(conn);
28+
29+
exit(1);
30+
}
31+
32+
PQclear(res);
33+
printf("Drop table OK!\n");
34+
35+
// Create new table.
36+
res = PQexec(conn,
37+
" CREATE TABLE example ( "
38+
" id INT, "
39+
" name VARCHAR(100) "
40+
" ); ");
41+
42+
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
43+
fprintf(stderr, "Create error, %s\n", PQerrorMessage(conn));
44+
45+
PQclear(res);
46+
PQfinish(conn);
47+
48+
exit(1);
49+
}
50+
51+
PQclear(res);
52+
printf("Create table OK!\n");
53+
2054
// Close connection.
2155
PQfinish(conn);
2256
printf("Disconnected!\n");

3 - insert item.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
// Insert new item.
21+
char *stm =
22+
" INSERT INTO example ( "
23+
" id, "
24+
" name "
25+
" ) VALUES ( "
26+
" $1, $2 "
27+
" ); ";
28+
29+
int total_items = 2;
30+
char *items []= {
31+
"1",
32+
"Test item"
33+
};
34+
35+
PGresult *res = PQexecParams(
36+
conn, stm, total_items, NULL, (const char * const*) items, NULL, NULL, 0);
37+
38+
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
39+
fprintf(stderr, "Insert error, %s\n", PQerrorMessage(conn));
40+
41+
PQclear(res);
42+
PQfinish(conn);
43+
44+
exit(1);
45+
}
46+
47+
printf("Item successfully inserted into the database.\n");
48+
49+
// Close connection.
50+
PQfinish(conn);
51+
printf("Disconnected!\n");
52+
53+
return 0;
54+
}
55+
56+

readme-pt.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ De uso geral:
8888
Códigos:
8989

9090
- <b>1 - connect.c</b>: Realiza a conexão com o servidor.
91-
- <b>2 - create table.c</b>: Cria uma nova tabela no banco de dados.
91+
- <b>2 - create table.c</b>: Cria e exclui uma tabela no banco de dados.
92+
- <b>3 - insert item.c</b>: Insere registros em uma tabela.
9293

9394
<br>
9495

0 commit comments

Comments
 (0)