Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this assignment, you'll practice accessing data in sqlite 3 database using Python. We will use a database of Pokemon as an example Use the
In this assignment, you'll practice accessing data in sqlite database using Python. We will
use a database of Pokemon as an example
Use the following codes as the basis for this exercise.
import sqlite
# create connection and cursor
conn sqlite connect pokemondb
cursor conn. cursor
# execute SQL query. In this example, show all data in pokemonmeta table
cursor.executeSELECT from pokemonmeta
rowscursor.fetchall
# print the column names of the table
header
for columninfo in cursor.description:
headercolumninfo
printheader
#print the table content
for row in rows:
printrow
#close connection after using it
conn.close
In the sample codes, cursor. description provides the column names of the last query
cursorexecuteSELECT from pokemonmeta in our case cursor.description returns a
list of tuple that contains the column information, and the first element of the tuple is the
name of the column. Hence when we iterate through the cursor.description list with
columninfo in the first forloop, columninfo refers to the tuples in the list one by one, and
columninfo refers to the name of a column
The program above reads from a table named pokemonmeta and show all the content of
the table. Note that in order to run the program, you need to download the pokemon.db file
attached to this assignment and place it in the same folder with the code file.
Task
points Extend the program so that it shows a list of all water pokemons instead of all
pokemons. Note either type or type can be "water". So the SQL command to be
used can be something like:
SELECT from pokemonmeta WHERE type'fire' OR
type'fire'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started