diff --git a/exercicios/para-casa/sistema/funcoes/__init__.py b/exercicios/para-casa/sistema/funcoes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/exercicios/para-casa/sistema/funcoes/__pycache__/__init__.cpython-312.pyc b/exercicios/para-casa/sistema/funcoes/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..3778ef2 Binary files /dev/null and b/exercicios/para-casa/sistema/funcoes/__pycache__/__init__.cpython-312.pyc differ diff --git a/exercicios/para-casa/sistema/funcoes/__pycache__/chamadas.cpython-312.pyc b/exercicios/para-casa/sistema/funcoes/__pycache__/chamadas.cpython-312.pyc new file mode 100644 index 0000000..cd6b227 Binary files /dev/null and b/exercicios/para-casa/sistema/funcoes/__pycache__/chamadas.cpython-312.pyc differ diff --git a/exercicios/para-casa/sistema/funcoes/__pycache__/entradas_usuario.cpython-312.pyc b/exercicios/para-casa/sistema/funcoes/__pycache__/entradas_usuario.cpython-312.pyc new file mode 100644 index 0000000..068df81 Binary files /dev/null and b/exercicios/para-casa/sistema/funcoes/__pycache__/entradas_usuario.cpython-312.pyc differ diff --git a/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios1.cpython-312.pyc b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios1.cpython-312.pyc new file mode 100644 index 0000000..c4bd96c Binary files /dev/null and b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios1.cpython-312.pyc differ diff --git a/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios2.cpython-312.pyc b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios2.cpython-312.pyc new file mode 100644 index 0000000..562104f Binary files /dev/null and b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios2.cpython-312.pyc differ diff --git a/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios3.cpython-312.pyc b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios3.cpython-312.pyc new file mode 100644 index 0000000..5962bad Binary files /dev/null and b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios3.cpython-312.pyc differ diff --git a/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios4.cpython-312.pyc b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios4.cpython-312.pyc new file mode 100644 index 0000000..0af58f6 Binary files /dev/null and b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios4.cpython-312.pyc differ diff --git a/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios5.cpython-312.pyc b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios5.cpython-312.pyc new file mode 100644 index 0000000..41fd157 Binary files /dev/null and b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios5.cpython-312.pyc differ diff --git a/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios6.cpython-312.pyc b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios6.cpython-312.pyc new file mode 100644 index 0000000..a171a38 Binary files /dev/null and b/exercicios/para-casa/sistema/funcoes/__pycache__/exercicios6.cpython-312.pyc differ diff --git a/exercicios/para-casa/sistema/funcoes/chamadas.py b/exercicios/para-casa/sistema/funcoes/chamadas.py new file mode 100644 index 0000000..958b949 --- /dev/null +++ b/exercicios/para-casa/sistema/funcoes/chamadas.py @@ -0,0 +1,12 @@ + +def primeira_chamada(): + + print("Vamos criar um banco de dados para gerenciar a livraria São Geraldo!\n\n\n") + print("1º Passo: CRIAR UM BANCO DE DADOS... nele podemos armazenar todos os livros que fazem parte do inventario da livraria\n\n") + + + +def segunda_chamada(): + print ("Agora vamos criar um arquivo CSV, siga as instruções a seguir de acordo com os dados que colocou anteriormente\n\n") + + diff --git a/exercicios/para-casa/sistema/funcoes/entradas_usuario.py b/exercicios/para-casa/sistema/funcoes/entradas_usuario.py new file mode 100644 index 0000000..4f0319f --- /dev/null +++ b/exercicios/para-casa/sistema/funcoes/entradas_usuario.py @@ -0,0 +1,82 @@ + +def nome_db(): + db = input("Digite o nome que você deseja dar para o banco de dados, com a extenção db (nome.db):\n") + return db + +def nome_tabela(): + tabela = input("Digite o nome que você deseja dar para a tabela:\n") + return tabela + +def colunas(): + colunas = input("Digite os nome das colunas da tabela que você criou, separadas por virgula e de acordo com o exemplo: (nome text not null, idade integer not null, sobrenome text not null)\n\n") + return colunas + +def col_csv(): + + colunas_csv = input("Digite as colunas que você deseja inserir em sua tabela separadas por vírgula (nome, idade,sobrenome):\n\n ") + lista_col_csv = [coluna.strip() for coluna in colunas_csv.split(",")] + + tabela_csv = [] + + for i in lista_col_csv: + tabela_csv.append(i) + + return tabela_csv + +def valores_csv(tabela_csv): + + tab_valores = [] + + controle = True + + while controle: + valores_csv = input(f"Digite os valoes para as colunas {tabela_csv}, separados por vígula, quando acabar uma linha, digite enter e escolha a opção\n\n") + lista_valores_csv = [valor.strip() for valor in valores_csv.split(",")] + + if len(tabela_csv) != len(lista_valores_csv): + print("Você inseriu uma quantidade de valores insuficientes para as colunas da tabela!\n\n") + else: + tab_valores.append(lista_valores_csv) + + opt = input("Você deseja inserir mais valores ? [1]Sim / [2]Não\n\n") + if int(opt) != 1: + controle = False + + return tab_valores + + +def opcoes(): + + opt_principal = input(" O que você deseja fazer:\n\n[1] Consultar dados\n\n[2] Atualizar dados\n\n[3] Deletar dados\n\n [4] Sair do menu de opções") + + return int(opt_principal) + + +def atualizar_dados(): + + opt1 = input("Você deseja atualizar o preço de algum livro?\n [1] SIM [2] NÃO") + + if int(opt1) == 1: + opt2 = input("Digite o Id do livro que você deseja atualizar\n") + opt3 = input("Digite o valor do preço\n") + else: + print("OK, vamos manter do mesmo jeito!") + + return opt2,opt3 + +def remover(): + opt1 = input("Você deseja remover algum livro?\n [1] SIM [2] NÃO\n") + + if int(opt1) == 1: + opt2 = input("Digite o Id do livro que você deseja remover\n") + else: + print("OK, vamos mater do mesmo jeito!\n") + + return int(opt2) + + +def exportar_csv(): + opt_csv = input("Você deseja exportar esses dados para um arquivo CSV? \n [1] SIm [2] NÃO\n ") + + return int(opt_csv) + diff --git a/exercicios/para-casa/sistema/funcoes/exercicios1.py b/exercicios/para-casa/sistema/funcoes/exercicios1.py new file mode 100644 index 0000000..5764509 --- /dev/null +++ b/exercicios/para-casa/sistema/funcoes/exercicios1.py @@ -0,0 +1,27 @@ + + +import sqlite3 + +def conectar_db(banco): + + + coon = sqlite3.connect(banco) + return coon + + + +def tabelas(coon, nome_tabela, *args): + cursor = coon.cursor() + + col = ', '.join(args) + + cursor.execute(f""" + CREATE TABLE IF NOT EXISTS {nome_tabela} ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + {col} + ) + """) + + coon.commit() + + \ No newline at end of file diff --git a/exercicios/para-casa/sistema/funcoes/exercicios2.py b/exercicios/para-casa/sistema/funcoes/exercicios2.py new file mode 100644 index 0000000..25fbfb3 --- /dev/null +++ b/exercicios/para-casa/sistema/funcoes/exercicios2.py @@ -0,0 +1,41 @@ + +import csv +import sqlite3 + +def criar_csv(tabela,linha_csv,col_csv,coon): + + cursor = coon.cursor() + + + with open(f'{tabela}', 'w', newline='', encoding='utf-8') as csvfile: + escritor = csv.writer(csvfile) + escritor.writerow(col_csv) + escritor.writerows(linha_csv) + + + with open(f'{tabela}', newline='', encoding='utf-8') as csvfile: + leitor = csv.reader(csvfile) + next(leitor) + for linha in leitor: + placeholders = ', '.join('?' * len(col_csv)) + sql = f"INSERT INTO livros ({', '.join(col_csv)}) VALUES ({placeholders})" + + try: + cursor.execute(sql, linha) + except sqlite3.OperationalError as e: + print(f"Erro ao inserir dados: {e}") + + coon.commit() + + + + + + + + + + + + + diff --git a/exercicios/para-casa/sistema/funcoes/exercicios3.py b/exercicios/para-casa/sistema/funcoes/exercicios3.py new file mode 100644 index 0000000..7ba29a5 --- /dev/null +++ b/exercicios/para-casa/sistema/funcoes/exercicios3.py @@ -0,0 +1,19 @@ + +import sqlite3 + +def colsulta_csv(coon): + + coon = sqlite3.connect('livraria.db') + cursor = coon.cursor() + + cursor.execute("select * from livros") + registros = cursor.fetchall() + + for registro in registros: + print(registro) + + coon.commit() + + + + return print(registro) diff --git a/exercicios/para-casa/sistema/funcoes/exercicios4.py b/exercicios/para-casa/sistema/funcoes/exercicios4.py new file mode 100644 index 0000000..3fb9c54 --- /dev/null +++ b/exercicios/para-casa/sistema/funcoes/exercicios4.py @@ -0,0 +1,14 @@ +import sqlite3 + +def atualizar(coon,opt2,opt3): + + coon = sqlite3.connect('livraria.db') + cursor = coon.cursor() + + id = opt2 + preco = opt3 + cursor.execute("update livros set id = ? where preco =?", (id,preco)) + + coon.commit() + + diff --git a/exercicios/para-casa/sistema/funcoes/exercicios5.py b/exercicios/para-casa/sistema/funcoes/exercicios5.py new file mode 100644 index 0000000..f902c14 --- /dev/null +++ b/exercicios/para-casa/sistema/funcoes/exercicios5.py @@ -0,0 +1,13 @@ + +import sqlite3 + +def delete(opt2,coon): + + coon = sqlite3.connect('livraria.db') + cursor = coon.cursor() + + cursor.execute("DELETE FROM livros where id = ?" ,(opt2)) + + + coon.commit() + diff --git a/exercicios/para-casa/sistema/funcoes/exercicios6.py b/exercicios/para-casa/sistema/funcoes/exercicios6.py new file mode 100644 index 0000000..b1c00a0 --- /dev/null +++ b/exercicios/para-casa/sistema/funcoes/exercicios6.py @@ -0,0 +1,18 @@ + +import csv +import sqlite3 + +def exportar_arquivo(coon, colunas): + + conn = sqlite3.connect('livraria.db') + cursor = conn.cursor() + + cursor.execute("SELECT * FROM livros") + dados = cursor.fetchall() + + with open('exportados_livros.csv', 'w', newline='', encoding='utf-8') as csvfile: + escritor = csv.writer(csvfile) + escritor.writerow(colunas) + escritor.writerows(dados) + + print("Aquivo exportado") diff --git a/exercicios/para-casa/sistema/livraria.db b/exercicios/para-casa/sistema/livraria.db new file mode 100644 index 0000000..08d12f8 Binary files /dev/null and b/exercicios/para-casa/sistema/livraria.db differ diff --git a/exercicios/para-casa/sistema/livros b/exercicios/para-casa/sistema/livros new file mode 100644 index 0000000..5947c5f --- /dev/null +++ b/exercicios/para-casa/sistema/livros @@ -0,0 +1,5 @@ +titulo,autor,ano,preco +Que o tempo não volta,Pamella Fernandes,2009,56.90 +Tudo de bom,Rafael Eleoterio,2007,34.99 +Tudo que ela gosta,Roseli de Abreu,2005,89.90 +O que eu mais quero,Caio Henrique,2001,78.90 diff --git a/exercicios/para-casa/sistema/livros.csv b/exercicios/para-casa/sistema/livros.csv new file mode 100644 index 0000000..5947c5f --- /dev/null +++ b/exercicios/para-casa/sistema/livros.csv @@ -0,0 +1,5 @@ +titulo,autor,ano,preco +Que o tempo não volta,Pamella Fernandes,2009,56.90 +Tudo de bom,Rafael Eleoterio,2007,34.99 +Tudo que ela gosta,Roseli de Abreu,2005,89.90 +O que eu mais quero,Caio Henrique,2001,78.90 diff --git a/exercicios/para-casa/sistema/principal.py b/exercicios/para-casa/sistema/principal.py new file mode 100644 index 0000000..0d5fe41 --- /dev/null +++ b/exercicios/para-casa/sistema/principal.py @@ -0,0 +1,47 @@ + + +from funcoes import chamadas, exercicios1, entradas_usuario, exercicios2, exercicios3, exercicios4, exercicios5, exercicios6 + +chamadas.primeira_chamada() +db = entradas_usuario.nome_db() +coon = exercicios1.conectar_db(db) +tabela = entradas_usuario.nome_tabela() +col = entradas_usuario.colunas() +chamadas.segunda_chamada() +exercicios1.tabelas(coon, tabela,col) +colunas_csv = entradas_usuario.col_csv() +linhas_csv = entradas_usuario.valores_csv(colunas_csv) +aquivo_csv = exercicios2.criar_csv(tabela,linhas_csv,colunas_csv,coon) + +controle = True + +opcoes = entradas_usuario.opcoes() +while controle: + if opcoes == 1: + consulta = exercicios3.colsulta_csv(coon) + elif opcoes == 2: + opt_id, opt_preco = entradas_usuario.atualizar_dados() + atualizar = exercicios4.atualizar(coon,opt_id,opt_preco) + elif opcoes == 3: + opt_remover = entradas_usuario.remover() + remover = exercicios5.delete(coon,opt_remover) + elif opcoes == 4: + controle = False + print("Você escolheu a opção sair!") + else: + continue + + +resp_exportar_csv = entradas_usuario.exportar_csv() +if resp_exportar_csv == 1: + exportados_csv = exercicios6.exportar_arquivo(coon,colunas_csv) +else: + print("Exportamos outro dia") + + +coon.close() + + + + +