-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#23: Adicionando classe Missão e funções de inserção, deleção e consulta
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import psycopg2 | ||
from database import Database | ||
|
||
class Missao: | ||
def __init__(self): | ||
self.db=Database() | ||
|
||
def inserirMissao(self, nome:str, chefe:bool, descricao:str, recompensa:int): | ||
try: | ||
conexao=self.db.conexao | ||
cursor=conexao.cursor() | ||
cursor.execute(f"""INSERT INTO Missao VALUES('{nome}', {chefe}, '{descricao}', {recompensa});""") | ||
conexao.commit() | ||
return print("Missão Inserida") | ||
except psycopg2.Error as e: | ||
print("Erro ao inserir Missão", e) | ||
finally: | ||
cursor.close() | ||
|
||
def consultarMissao(self): | ||
try: | ||
conexao=self.db.conexao | ||
cursor=conexao.cursor() | ||
cursor.execute(f"""SELECT * FROM Missao;""") | ||
conexao.commit() | ||
resultado=cursor.fetchall() | ||
for i in resultado: | ||
print(i) | ||
except psycopg2.Error as e: | ||
print("Erro ao consultar Missão", e) | ||
finally: | ||
cursor.close() | ||
|
||
def deletarMissao(self, nome:str): | ||
try: | ||
conexao=self.db.conexao | ||
cursor=conexao.cursor() | ||
cursor.execute(f"""DELETE FROM Missao WHERE NOME = '{nome}';""") | ||
conexao.commit() | ||
return print("Missão Deletada") | ||
except psycopg2.Error as e: | ||
print("Erro ao deletar Missão", e) | ||
finally: | ||
cursor.close() | ||
|
||
def consultarMissaoNome(self, nome:str): | ||
try: | ||
conexao=self.db.conexao | ||
cursor=conexao.cursor() | ||
cursor.execute(f"""SELECT * FROM Missao;""") | ||
conexao.commit() | ||
resultado=cursor.fetchall() | ||
print(resultado[0]) | ||
except psycopg2.Error as e: | ||
print("Erro ao consultar Missão", e) | ||
finally: | ||
cursor.close() | ||
|
||
""" Data = Database() | ||
m = Missao() | ||
m.inserirMissao('Salvar pessoas', True, 'Salve pessoas', 100) | ||
m.deletarMissao('Salvar pessoas') | ||
m.consultarMissao() | ||
m.consultarMissaoNome('Salvar pessoas') """ |