Question: import mysql . connector import sys import time # Create a database connection def create _ connection ( ) : Create a

import mysql.connector
import sys
import time
# Create a database connection
def create_connection():
"""
Create a connection to 'zybooksdb' database
:return: Connection object
"""
# YOUR CODE HERE
conn = mysql.connector.connect(
user = 'root',
host ='127.0.0.1',
database = 'zybooksdb'
)
return conn
# Create Horse table
def create_table(conn):
"""
Create Horse table
:param conn: Connection object
:return: Nothing
"""
cursor = conn.cursor()
# YOUR CODE HERE
cursor.execute("DROP TABLE IF EXISTS Horse")
cursor.execute("""
CREATE TABLE Horse (
ID INT PRIMARY KEY,
Nmae VARCHAR(30),
Breed VARCHAR(30),
Height DOUBLE,
BirthDatr VARCHAR(10)
)
""")
# Insert row to Horse table using paramenters
def insert_horse(conn, horse_data):
"""
Create a new row in Horse table
:param conn: Connection object
:param data: Tuple of values to be inserted in row
:return: Nothing
"""
# YOUR CODE HERE
cursor = conn.cursor()
insert_query="""
INSERT INTO Horse (ID, Name, Breed, Height, BirthDate)
VALUES (%s,%s,%s,%s,%s)
"""
#horse_data =(1, 'Babe', 'Quarter Horse', 15.3,'2015-02,10')
cursor.execute(insert_query, horse_data)
conn.commit()
# Select and print all rows of Horse table
def select_all_horses(conn):
"""
Query all rows in the Horse table
:param conn: Connection object
:return: Nothing
"""
# YOUR CODE HERE
cursor = conn.CURSOR()
cursor.execute("select * FROM Horse")
rows = cursor.fetchall()
for row in rows:
Print(row)
# DO NOT MODIFY main
if __name__=='__main__':
# Create database connection
conn = create_connection()
if conn is None:
exit()
# Create Horse table
create_table(conn)
# Insert row to Horse table
horse_data =(1, "Babe", "Quarter Horse", 15.3,"2015-02-10")
insert_horse(conn, horse_data)
# Select and print all Horse table rows
select_all_horses(conn)
conn.close()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!