5.8 LAB - Database programming with Python (SQLite) Complete the Python program to create a Horse table, insert one row,
Posted: Fri May 20, 2022 3:00 pm
The program output should be: All horses: (1, 'Babe', 'Quarter Horse', 15.3, '2015-02-10') This lab uses the SQLite database rather than MySQL. The Python API for SQLite is similar to MySQL Connector/Python. Consequently, the API is as described in the text, with a few exceptions: • Use the import library provided in the program template. • Create a connection object with the function sqlite3.connect(":memory:") • Use the character ? instead of %s as a placeholder for query parameters. • Use data type text instead of char and varchar. SQLite reference information can be found at SQLite Python Tutorial, but is not necessary to complete this lab. 403894.1874424.qx3zqy7 LAB ACTIVITY 5.8.1: LAB - Database programming with Python (SQLite) 3/ 10 main.py Load default template... 1 import sqlite3 2 from sqlite3 import Error 4 # Creates connection to sqlite in-memory database 5 def create_connection: 7 8 Create a connection to in-memory database return: Connection object 10 11 # YOUR CODE HERE 12 # Use sqlite3.connect(":memory:") to create connection object 13 14 return conn 15 16 17 # Creates Horse table 18 def create_table (conn) : Develop mode Submit mode Run your program as often as you'd like, before submitting for grading. Below, type any needed input values in the first box, then click Run program and observe the program's output in the
24 17 # Creates Horse table 18 def create_table conn) : 19 20 Create Horse table 21 param conn: Connection object 22 return: Nothing 23 24 25 # YOUR CODE HERE 26 27 28 # Inserts row to Horse table given data tuple 29 def insert_horse (conn, data): 30 31 Create a new row in Horse table 32 param conn: Connection object 33 param data: tuple of values for new row 34 :return: Nothing 35 36 37 # YOUR CODE HERE 38 # Use the ? character as placeholder for SQLite query parameters 39 40 41 # Selects and prints all rows of Horse table 42 def select_all_horses (conn): 43 Query all rows in the Horse table 45 param conn: the Connection object 46 :return: Nothing 47 48 49 # YOUR CODE HERE 44 50 51