-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_prepare.py
70 lines (59 loc) · 2.31 KB
/
database_prepare.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import psycopg
from psycopg import sql
from flask_bcrypt import generate_password_hash
# Conecta ao banco de dados existente
with psycopg.connect(host="localhost", dbname="postgres", user="sasuke", password="postgres") as conn:
conn.autocommit = True
database_name = "martial_arts_library"
with conn.cursor() as cur:
# Cria um novo banco de dados
cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(database_name)))
# Conecta ao novo banco de dados criado
with psycopg.connect(host="localhost", dbname=database_name, user="sasuke", password="postgres") as conn:
with conn.cursor() as cur:
# Cria a tabela 'martial_arts'
cur.execute("""
CREATE TABLE IF NOT EXISTS martial_arts (
id serial PRIMARY KEY,
name text,
points integer,
category text
)
""")
# Cria a tabela 'users'
cur.execute("""
CREATE TABLE IF NOT EXISTS users (
id serial PRIMARY KEY,
name text,
nickname text,
password text
)
""")
# Insere dados na tabela 'martial_arts'
cur.execute(
"INSERT INTO martial_arts (name, points, category) VALUES (%s, %s, %s)",
('Capoeira', 7, 'Dance')
)
cur.execute(
"INSERT INTO martial_arts (name, points, category) VALUES (%s, %s, %s)",
('Jiu Jitsu BR', 9, 'Grappling')
)
cur.execute(
"INSERT INTO martial_arts (name, points, category) VALUES (%s, %s, %s)",
('Karate', 9, 'Striking')
)
# Insere dados na tabela 'users'
cur.execute(
"INSERT INTO users (name, nickname, password) VALUES (%s, %s, %s)",
('Izuku Midoriya', 'midoriya', generate_password_hash("hero").decode('utf-8'))
)
cur.execute(
"INSERT INTO users (name, nickname, password) VALUES (%s, %s, %s)",
('Bakugo Katsuki', 'bakugo', generate_password_hash("explosion").decode('utf-8'))
)
cur.execute(
"INSERT INTO users (name, nickname, password) VALUES (%s, %s, %s)",
('Uchiha Sasuke', 'sasuke', 'sharingan')
)
# Salva as mudanças no banco de dados
conn.commit()