Passos Iniciais Realizados Nesse Bootcamp Python AI Backend Developer. dio_me
Slides: Banco de Dados. Desafio.
import sqlite3
from pathlib import Path
ROOT_PATH = Path(__file__).parent
conexao = sqlite3.connect(ROOT_PATH / "meu_banco.sqlite")
cursor = conexao.cursor()
cursor.row_factory = sqlite3.Row
def criar_tabela(conexao, cursor):
cursor.execute(
"CREATE TABLE clientes (id INTEGER PRIMARY KEY AUTOINCREMENT, nome VARCHAR(100), email VARCHAR(150))"
)
conexao.commit()
def inserir_registro(conexao, cursor, nome, email):
data = (nome, email)
cursor.execute("INSERT INTO clientes (nome, email) VALUES (?,?);", data)
conexao.commit()
def atualizar_registro(conexao, cursor, nome, email, id):
data = (nome, email, id)
cursor.execute("UPDATE clientes SET nome=?, email=? WHERE id=?;", data)
conexao.commit()
def excluir_registro(conexao, cursor, id):
data = (id,)
cursor.execute("DELETE FROM clientes WHERE id=?;", data)
conexao.commit()
def inserir_muitos(conexao, cursor, dados):
cursor.executemany("INSERT INTO clientes (nome, email) VALUES (?,?)", dados)
conexao.commit()
def recuperar_cliente(cursor, id):
cursor.execute("SELECT email, id, nome FROM clientes WHERE id=?", (id,))
return cursor.fetchone()
def listar_clientes(cursor):
return cursor.execute("SELECT * FROM clientes ORDER BY nome DESC;")
clientes = listar_clientes(cursor)
for cliente in clientes:
print(dict(cliente))
cliente = recuperar_cliente(cursor, 2)
print(dict(cliente))
print(cliente["id"], cliente["nome"], cliente["email"])
print(f'Seja bem vindo ao sistema {cliente["nome"]}')
# dados = [
# ("Guilherme", "guilherme@gmail.com"),
# ("Chappie", "chappie@gmail.com"),
# ("Melaine", "melaine@gmail.com"),
# ]
# inserir_muitos(conexao, cursor, dados)
import sqlite3
from pathlib import Path
ROOT_PATH = Path(__file__).parent
conexao = sqlite3.connect(ROOT_PATH / "meu_banco.sqlite")
cursor = conexao.cursor()
cursor.row_factory = sqlite3.Row
id_cliente = input("Informe o id do cliente: ")
cursor.execute(f"SELECT * FROM clientes WHERE id={id_cliente}")
clientes = cursor.fetchall()
for cliente in clientes:
print(dict(cliente))
import sqlite3
from pathlib import Path
ROOT_PATH = Path(__file__).parent
conexao = sqlite3.connect(ROOT_PATH / "meu_banco.sqlite")
cursor = conexao.cursor()
cursor.row_factory = sqlite3.Row
try:
cursor.execute("DELETE FROM clientes WHERE id = 8;")
conexao.commit()
cursor.execute("INSERT INTO clientes (nome, email) VALUES (?,?)", ("Teste 3", "teste3@gmail.com"))
cursor.execute("INSERT INTO clientes (id, nome, email) VALUES (?,?,?)", (2, "Teste 4", "teste4@gmail.com"))
conexao.commit()
except Exception as exc:
print(f"Ops! um erro ocorreu! {exc}")
conexao.rollback()