Answered step by step
Verified Expert Solution
Link Copied!

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 sqlite3 database using Python. We will
use a database of Pokemon as an example
Use the following codes as the basis for this exercise.
import sqlite3
# create connection and cursor
conn = sqlite3. connect ('pokemon.db')
cursor = conn. cursor ()
# execute SQL query. In this example, show all data in [pokemon_meta] table
cursor.execute("SELECT * from [pokemon_meta]")
rows=cursor.fetchall()
# print the column names of the table
header=""
for column_info in cursor.description:
header+=column_info[
print(header)
#print the table content
for row in rows:
print(row)
#close connection after using it
conn.close()
In the sample codes, cursor. description provides the column names of the last query
(cursor.execute("SELECT* from [pokemon_meta]") 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
column_info in the first for-loop, column_info refers to the tuples in the list one by one, and
column_info[0] refers to the name of a column
The program above reads from a table named [pokemon_meta], 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 1
(2points) Extend the program so that it shows a list of all water pokemons instead of all
pokemons. Note either type_1 or type_2 can be "water". So the SQL command to be
used can be something like:
SELECT * from [pokemon_meta] WHERE [type_1]='fire' OR
[type_2]='fire'
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago