-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate.py
More file actions
47 lines (38 loc) · 981 Bytes
/
Copy pathCreate.py
File metadata and controls
47 lines (38 loc) · 981 Bytes
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
39
40
41
42
43
44
45
46
47
import psycopg2
# Configuração da conexão
conn = psycopg2.connect(
host='localhost',
database='postgresDB',
user='postgres',
password='123'
)
cursor = conn.cursor()
# Criação da tabela (sem a parte do OWNER)
cursor.execute("""
CREATE TABLE IF NOT EXISTS public."AGENDA"
(
id integer NOT NULL,
nome text NOT NULL,
telefone char(12) NOT NULL
);
""")
# Inserir dados na tabela
cursor.execute("""
INSERT INTO public."AGENDA" (id, nome, telefone)
VALUES (1, 'teste 1', '02199999999');
""")
cursor.execute("""
INSERT INTO public."AGENDA" (id, nome, telefone)
VALUES (2, 'teste 2', '02188888888');
""")
conn.commit()
# Ler os dados
cursor.execute("""
SELECT id, nome, telefone FROM public."AGENDA";
""")
rows = cursor.fetchall()
for row in rows:
print(f"ID: {row[0]}, Nome: {row[1]}, Telefone: {row[2]}")
# Fechar a conexão
cursor.close()
conn.close()