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

Resolução Carla #12

Open
wants to merge 2 commits 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
15 changes: 15 additions & 0 deletions exercicios/para-casa/exportados_livros.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
titulo,autor,ano,preco
1,Dom Casmurro,Machado de Assis,1899,25.9
2,Grande Sertão: Veredas,João Guimarães Rosa,1956,42.0
3,O Guarani,José de Alencar,1857,19.9
4,Memórias Póstumas de Brás Cubas,Machado de Assis,1881,29.9
5,Capitães da Areia,Jorge Amado,1937,34.9
6,A Hora da Estrela,Clarice Lispector,1977,22.5
7,O Cortiço,Aluísio Azevedo,1890,27.9
8,Vidas Secas,Graciliano Ramos,1938,28.5
9,Iracema,José de Alencar,1865,18.0
10,O Auto da Compadecida,Ariano Suassuna,1955,23.0
11,Senhora,José de Alencar,1875,21.5
12,Macunaíma,Mário de Andrade,1928,28.7
13,A Moreninha,Joaquim Manuel de Macedo,1844,20.0
15,Lucíola,José de Alencar,1862,24.5
Binary file added exercicios/para-casa/livraria.db
Binary file not shown.
16 changes: 16 additions & 0 deletions exercicios/para-casa/livros.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
titulo,autor,ano,preco
Dom Casmurro,Machado de Assis,1899,25.9
Grande Sertão: Veredas,João Guimarães Rosa,1956,42.0
O Guarani,José de Alencar,1857,19.9
Memórias Póstumas de Brás Cubas,Machado de Assis,1881,29.9
Capitães da Areia,Jorge Amado,1937,34.9
A Hora da Estrela,Clarice Lispector,1977,22.5
O Cortiço,Aluísio Azevedo,1890,27.9
Vidas Secas,Graciliano Ramos,1938,28.5
Iracema,José de Alencar,1865,18.0
O Auto da Compadecida,Ariano Suassuna,1955,23.0
Senhora,José de Alencar,1875,21.5
Macunaíma,Mário de Andrade,1928,30.0
A Moreninha,Joaquim Manuel de Macedo,1844,20.0
Quarup,Antonio Callado,1967,35.0
Lucíola,José de Alencar,1862,24.5
21 changes: 21 additions & 0 deletions exercicios/para-casa/script1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sqlite3

# Conectar ao banco de dados (ou criar um novo banco se não existir)
conn = sqlite3.connect('livraria.db')
cursor = conn.cursor()

# Criar uma tabela para armazenar os livros
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 NOT NULL
)
''')

#fechar comit
conn.commit()
#fechar cursor
conn.close()
26 changes: 26 additions & 0 deletions exercicios/para-casa/script2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import csv

# Dados dos 15 melhores livros brasileiros com preços estimados
livros = [
{"titulo": "Dom Casmurro", "autor": "Machado de Assis", "ano": 1899, "preco": 25.90},
{"titulo": "Grande Sertão: Veredas", "autor": "João Guimarães Rosa", "ano": 1956, "preco": 42.00},
{"titulo": "O Guarani", "autor": "José de Alencar", "ano": 1857, "preco": 19.90},
{"titulo": "Memórias Póstumas de Brás Cubas", "autor": "Machado de Assis", "ano": 1881, "preco": 29.90},
{"titulo": "Capitães da Areia", "autor": "Jorge Amado", "ano": 1937, "preco": 34.90},
{"titulo": "A Hora da Estrela", "autor": "Clarice Lispector", "ano": 1977, "preco": 22.50},
{"titulo": "O Cortiço", "autor": "Aluísio Azevedo", "ano": 1890, "preco": 27.90},
{"titulo": "Vidas Secas", "autor": "Graciliano Ramos", "ano": 1938, "preco": 28.50},
{"titulo": "Iracema", "autor": "José de Alencar", "ano": 1865, "preco": 18.00},
{"titulo": "O Auto da Compadecida", "autor": "Ariano Suassuna", "ano": 1955, "preco": 23.00},
{"titulo": "Senhora", "autor": "José de Alencar", "ano": 1875, "preco": 21.50},
{"titulo": "Macunaíma", "autor": "Mário de Andrade", "ano": 1928, "preco": 30.00},
{"titulo": "A Moreninha", "autor": "Joaquim Manuel de Macedo", "ano": 1844, "preco": 20.00},
{"titulo": "Quarup", "autor": "Antonio Callado", "ano": 1967, "preco": 35.00},
{"titulo": "Lucíola", "autor": "José de Alencar", "ano": 1862, "preco": 24.50}
]

# Criar o arquivo CSV
with open('livros.csv', mode='w', newline='', encoding='utf-8') as file:
writer = csv.DictWriter(file, fieldnames=["titulo", "autor", "ano", "preco"])
writer.writeheader()
writer.writerows(livros)
35 changes: 35 additions & 0 deletions exercicios/para-casa/script3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import csv
import sqlite3

# Conectar ao banco de dados (ou criar um novo banco se não existir)
conn = sqlite3.connect('livraria.db')
cursor = conn.cursor()

# Criar a tabela se ela não existir
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 NOT NULL
)
''')

# Ler os dados do arquivo CSV
with open('livros.csv', mode='r', encoding='utf-8') as file:
leitor = csv.DictReader(file)
livros = [(row['titulo'], row['autor'], int(row['ano']), float(row['preco'])) for row in leitor]

# Inserir os dados na tabela
cursor.executemany('''
INSERT INTO livros (titulo, autor, ano, preco) VALUES (?, ?, ?, ?)
''', livros)

# Confirmar as mudanças
conn.commit()

# Fechar a conexão com o banco de dados
conn.close()

# Fechar Cursor
17 changes: 17 additions & 0 deletions exercicios/para-casa/script4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sqlite3

# Conectar ao banco de dados
conn = sqlite3.connect('livraria.db')
cursor = conn.cursor()

# Selecionar todos os registros da tabela livros
cursor.execute('SELECT * FROM livros')
rows = cursor.fetchall()

# Exibir os registros
print("Lista dos livros no banco de dados:")
for row in rows:
print(f"ID: {row[0]}, Título: {row[1]}, Autor: {row[2]}, Ano: {row[3]}, Preço: R$ {row[4]:.2f}")

# Fechar a conexão com o banco de dados
conn.close()
10 changes: 10 additions & 0 deletions exercicios/para-casa/script5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sqlite3

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

cursor.execute("update livros set preco = ? where id = ?", (28.70,12))

conn.commit()
cursor.close()
conn.close()
10 changes: 10 additions & 0 deletions exercicios/para-casa/script6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sqlite3

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

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

conn.commit()
cursor.close()
conn.close()
16 changes: 16 additions & 0 deletions exercicios/para-casa/script7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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(['titulo','autor','ano','preco'])
escritor.writerows(dados)

cursor.close()
conn.close()
Binary file added exercicios/para-sala/csv/empresa.db
Binary file not shown.
6 changes: 6 additions & 0 deletions exercicios/para-sala/csv/exportados_clientes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id,nome,email
1,João,[email protected]
2,Maria,[email protected]
3,Pedro,[email protected]
4,Luiza,[email protected]
5,Fernando,[email protected]
7 changes: 7 additions & 0 deletions exercicios/para-sala/csv/script1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import csv


with open('produtos.csv', newline='', encoding='utf-8') as csvfile:
leitor = csv.reader(csvfile)
for linha in leitor:
print(linha)
23 changes: 23 additions & 0 deletions exercicios/para-sala/csv/script2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sqlite3
import csv

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

cursor.execute("""
CREATE TABLE IF NOT EXISTS clientes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nome TEXT NOT NULL,
email TEXT NOT NULL
)
""")

with open('clientes.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 clientes (nome, email) VALUES (?, ?)", (linha[1], linha[2]))

conn.commit()
cursor.close()
conn.close()
17 changes: 17 additions & 0 deletions exercicios/para-sala/csv/script3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sqlite3
import csv

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

cursor.execute("SELECT * FROM clientes")
dados = cursor.fetchall()

with open('exportados_clientes.csv', 'w', newline='', encoding='utf-8') as csvfile:
escritor = csv.writer(csvfile)
escritor.writerow(['id', 'nome', 'email'])
escritor.writerows(dados)

cursor.close()
conn.close()
cursor.close()
Binary file added exercicios/para-sala/exemplo/empresa.db
Binary file not shown.
Binary file added exercicios/para-sala/exemplo/exemplo.db
Binary file not shown.
24 changes: 24 additions & 0 deletions exercicios/para-sala/exemplo/script1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sqlite3

conn = sqlite3.connect("exemplo.db")
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS usuarios (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nome TEXT NOT NULL,
idade INTEGER NOT NULL
)

""")

cursor.execute ("insert into usuarios (nome, idade) values ('joao', 43)")
cursor.execute ("insert into usuarios (nome, idade) values ('jessica', 23)")

cursor.execute ("select * from usuarios")
registros = cursor.fetchall()

for registro in registros:
print(registros)

conn.commit()
conn.close()
23 changes: 23 additions & 0 deletions exercicios/para-sala/exemplo/script2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sqlite3
import csv

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

cursor.execute("""
CREATE TABLE IF NOT EXISTS clientes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nome TEXT NOT NULL,
email TEXT NOT NULL
)
""")

with open('clientes.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 clientes (nome, email) VALUES (?, ?)", (linha[1], linha[2]))

conn.commit()
cursor.close()
conn.close()
Binary file added exercicios/para-sala/exercicio/escola.db
Binary file not shown.
16 changes: 16 additions & 0 deletions exercicios/para-sala/exercicio/script2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sqlite3

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

cursor.execute("""
CREATE TABLE IF NOT EXISTS estudantes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nome TEXT NOT NULL,
idade INTEGER NOT NULL
)
""")

conn.commit()
cursor.close()
conn.close()
22 changes: 22 additions & 0 deletions exercicios/para-sala/exercicio/script3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sqlite3

# Criar conexão
conn = sqlite3.connect('escola.db')
# cursor para uso do SQL
cursor = conn.cursor()

#Criar tabela de insersão de dados
estudantes = [
('Alice', 21),
('Bob', 22),
('Charlie', 23)
]

#inserir varios dados de uma vez
cursor.executemany("INSERT INTO estudantes (nome, idade) VALUES (?, ?)", estudantes)

#fechar conexão
conn.commit()
#fechar cursor
cursor.close()
conn.close()
23 changes: 23 additions & 0 deletions exercicios/para-sala/exercicio/script4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sqlite3

#criar conexão
conn = sqlite3.connect('escola.db')
# criar cursor para uso do SQL
cursor = conn.cursor()

nome = input("Digite um nome: ")


#executar a função no SQL
cursor.execute("SELECT nome FROM estudantes WHERE nome = ? ", (nome,))
registros = cursor.fetchall()

#uso do For para ver registros no python
for registro in registros:
print(registros)

#Fechar cursor
cursor.close()
#fechar conexão
conn.close()

10 changes: 10 additions & 0 deletions exercicios/para-sala/exercicio/script5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sqlite3

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

cursor.execute("update estudantes set idade = ? where nome = ?", (23,'bob'))

conn.commit()
cursor.close()
conn.close()
10 changes: 10 additions & 0 deletions exercicios/para-sala/exercicio/script6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sqlite3

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

cursor.execute("DELETE FROM estudantes WHERE nome = ?", ('Charlie',))

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