Skip to content
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
173 changes: 173 additions & 0 deletions exercicios/para-casa/Banco de dados Mari.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "3fb51551",
"metadata": {},
"source": [
"## Banco de dados"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "6f065d6e",
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"import sqlite3"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "a7aa9973",
"metadata": {},
"outputs": [],
"source": [
"file = open('books_data_clean.csv', 'r', encoding='utf-8') "
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "eaaee9c6",
"metadata": {},
"outputs": [],
"source": [
"conteudo = csv.reader(file)\n",
"\n",
"connection = sqlite3.connect(\"banco_livros.db\")\n",
"cursor = connection.cursor()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "25d07ce5",
"metadata": {},
"outputs": [],
"source": [
"cursor.execute('''\n",
" CREATE TABLE IF NOT EXISTS books (\n",
" [Publishing Year] INT,\n",
" [Book Name] VARCHAR,\n",
" author VARCHAR,\n",
" language_code VARCHAR,\n",
" author_rating VARCHAR,\n",
" book_average_rating FLOAT,\n",
" book_ratings_count INT,\n",
" genre VARCHAR,\n",
" gross_sales FLOAT,\n",
" publisher_revenue FLOAT,\n",
" sale_price FLOAT,\n",
" sales_rank FLOAT,\n",
" publisher VARCHAR,\n",
" units_sold FLOAT\n",
" )\n",
"''')\n",
"\n",
"connection.commit()"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "20010962",
"metadata": {},
"outputs": [
{
"ename": "OperationalError",
"evalue": "table books has no column named Publishing Year",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mOperationalError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[27], line 21\u001b[0m\n\u001b[0;32m 1\u001b[0m inserir_conteudo \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'''\u001b[39m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;124m INSERT INTO books (\u001b[39m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;124m [Publishing Year],\u001b[39m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[38;5;124m VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\u001b[39m\n\u001b[0;32m 19\u001b[0m \u001b[38;5;124m'''\u001b[39m\n\u001b[1;32m---> 21\u001b[0m \u001b[43mcursor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecutemany\u001b[49m\u001b[43m(\u001b[49m\u001b[43minserir_conteudo\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconteudo\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 23\u001b[0m connection\u001b[38;5;241m.\u001b[39mcommit()\n",
"\u001b[1;31mOperationalError\u001b[0m: table books has no column named Publishing Year"
]
}
],
"source": [
"inserir_conteudo = '''\n",
" INSERT INTO books (\n",
" [Publishing Year],\n",
" book_name,\n",
" author,\n",
" language_code,\n",
" author_rating,\n",
" book_average_rating,\n",
" book_ratings_count,\n",
" genre,\n",
" gross_sales,\n",
" publisher_revenue,\n",
" sale_price,\n",
" sales_rank,\n",
" publisher,\n",
" units_sold\n",
" )\n",
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\n",
"'''\n",
"\n",
"cursor.executemany(inserir_conteudo, conteudo)\n",
"\n",
"connection.commit()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51ed46b3",
"metadata": {},
"outputs": [],
"source": [
"selecionar_tudo = \"SELECT * FROM books\"\n",
"entradas = cursor.execute(selecionar_tudo).fetchall()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f30ba732",
"metadata": {},
"outputs": [],
"source": [
"for entrada in entradas:\n",
" print(entrada)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a7dcca72",
"metadata": {},
"outputs": [],
"source": [
"connection.commit()\n",
"connection.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading