Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercicio-analunguinho #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions exercicios/para-casa/exercicio-casa/atualizar_livro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 4.**Atualização de Dados**
# - Escreva um script Python que atualize o preço de um livro específico (por exemplo, mude o preço do livro com `id` 1 para 29.99).

import sqlite3
import csv

conn = sqlite3.connect('livraria.db')
cursor = conn.cursor()

cursor.execute("UPDATE livros SET preco = ? WHERE id = ?", (29.99, 1))
registros = cursor.fetchall()

for registro in registros:
print(registro)

conn.commit()
cursor.close()
conn.close()
17 changes: 17 additions & 0 deletions exercicios/para-casa/exercicio-casa/consultar_livros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 3.**Consulta e Exibição de Dados**
#- Escreva um script Python que selecione e exiba todos os registros da tabela `livros`.

import sqlite3
import csv

conn = sqlite3.connect('livraria.db')
cursor = conn.cursor()

cursor.execute("SELECT * FROM livros")
registros = cursor.fetchall()

for registro in registros:
print(registro)

cursor.close()
conn.close()
21 changes: 21 additions & 0 deletions exercicios/para-casa/exercicio-casa/criar_banco.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 1.**Criação do Banco de Dados e Tabela**

import sqlite3
import csv

conn = sqlite3.connect("livraria.db")
cursor = conn.cursor()

cursor.execute("""

CREATE TABLE IF NOT EXISTS livros (
id INTEGER PRIMARY KEY AUTOINCREMENT,
titulo TEXT NOT NULL,
autor TEXT NOT NULL,
ano INTEGER NOT NULL,
preco REAL
)
""")

conn.commit()
conn.close()
8 changes: 8 additions & 0 deletions exercicios/para-casa/exercicio-casa/exportados_livros.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
id,titulo,autor,ano,preco
1,Dementia 21 vol. I,Shintaro Kago,2020,29.99
2,Nas Montanhas do Terror, Junpei Azumi,2020,45.46
4,O Estranho Conto da Ilha Panorama,Suehiro Maruo,2007,49.7
5,Crazy Food Truck vol.I,Rokurou Ogaki,2024,48.9
6,Dementia 21 vol. II,Shintaro Kago,2023,59.9
7,O Diário do Meu Pai,Jiro Taniguchi,2024,82.93
8,FIRE! vol.I,Hideko Mizuno,2024,76.9
19 changes: 19 additions & 0 deletions exercicios/para-casa/exercicio-casa/exportar_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 6.**Exportação de Dados para CSV**
#- Escreva um script Python que exporte os dados da tabela `livros` para um novo arquivo CSV chamado `exportados_livros.csv`.

import sqlite3
import csv

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(['id', 'titulo', 'autor', 'ano', 'preco'])
escritor.writerows(dados)

cursor.close()
conn.close()
28 changes: 28 additions & 0 deletions exercicios/para-casa/exercicio-casa/importar_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 2.**Importação de Dados de um CSV**

import sqlite3
import csv

conn = sqlite3.connect('livraria.db')
cursor = conn.cursor()

cursor.execute("""

CREATE TABLE IF NOT EXISTS livros (
id INTEGER PRIMARY KEY AUTOINCREMENT,
titulo TEXT,
autor TEXT,
ano INTEGER,
preco REAL NOT NULL
)
""")

with open('livros.csv', newline='', encoding='utf-8') as csvfile:
leitor = csv.reader(csvfile)
next(leitor) # Pular o cabeçalho
for linha in leitor:
cursor.execute("INSERT INTO livros (titulo, autor, ano, preco) VALUES (?, ?, ?, ?)", (linha[0], linha[1], linha[2], linha[3]))

conn.commit()
cursor.close()
conn.close()
Binary file not shown.
9 changes: 9 additions & 0 deletions exercicios/para-casa/exercicio-casa/livros.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
titulo,autor,ano,preco
Dementia 21 vol. I,Shintaro Kago,2020,74.93
Nas Montanhas do Terror, Junpei Azumi,2020,45.46
Cidade das Lápides,Junji Ito,2023,73.00
O Estranho Conto da Ilha Panorama,Suehiro Maruo,2007,49.70
Crazy Food Truck vol. I,Rokurou Ogaki,2024,48.90
Dementia 21 vol. II,Shintaro Kago,2023,59.90
O Diário do Meu Pai,Jiro Taniguchi,2024,82.93
FIRE! vol. I,Hideko Mizuno,2024,76.90
14 changes: 14 additions & 0 deletions exercicios/para-casa/exercicio-casa/remover_livro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 5.**Remoção de Dados**
# - Escreva um script Python que remova um livro específico da tabela (por exemplo, remova o livro com `id` 3).

import sqlite3
import csv

conn = sqlite3.connect('livraria.db')
cursor = conn.cursor()

cursor.execute("DELETE FROM livros WHERE id = ?", ('3',))

conn.commit()
cursor.close()
conn.close()