write a Python program that connects to a sqlite database. create a table called Horses with the
Question:
write a Python program that connects to a sqlite database.
create a table called Horses with the following fields:
- id (integer): a primary key and not null
- name (text)
- breed (text)
- height (real)
- birthday (text)
Next, insert the following data row into the Horses table:
id: 1 name: 'Babe' breed: 'Quarter Horse' height: 15.3 birthday: '2015-02-10'
Output all records from the Horses table.
Ex: With the above row inserted, the output should be:
All Horses: (1, 'Babe', 'Quarter Horse', 15.3, '2015-02-10')
import sqlite3
from sqlite3 import Error
# NOTE: Do not modify
# Creates a sqlite3 database connection (in memory)
def create_connection(db_file):
""" create a database connection to a SQLite database """
conn = None
try:
conn = sqlite3.connect(":memory:")
return conn
except Error as e:
print(e)
return conn
# Creates a table
def create_table(conn, create_table_sql):
# FIXME: type your code here
# Inserts horse data as a row in the database
def insert_horse(conn, data):
# FIXME: type your code here
# Prints all rows of data fromthe horses table
def select_all_horses(conn):
# FIXME: type your code here
if __name__ == '__main__':
database = r"HorseStable.db"
# FIXME 1: Call the create_connection function to connect to the database HorseStable.db
# FIXME 2: Create the sql statement string to create A table
# FIXME 3: Call the create_table function passing the sql statement string
# FIXME 4: Insert the horse data into the database
# FIXME 5: Print out all hourses by calling the select_all_horses function
Starting Out With Java From Control Structures Through Data Structures
ISBN: 9780133957051
6th Edition
Authors: Tony Gaddis