112 lines
3.7 KiB
Python
112 lines
3.7 KiB
Python
########################### DO NOT MODIFY THIS SECTION ##########################
|
|
#################################################################################
|
|
import sqlite3
|
|
from sqlite3 import Error
|
|
import csv
|
|
#################################################################################
|
|
|
|
## Change to False to disable Sample
|
|
SHOW = True
|
|
|
|
############### SAMPLE CLASS AND SQL QUERY ###########################
|
|
######################################################################
|
|
class Sample():
|
|
def sample(self):
|
|
try:
|
|
connection = sqlite3.connect("sample")
|
|
connection.text_factory = str
|
|
except Error as e:
|
|
print("Error occurred: " + str(e))
|
|
print('\033[32m' + "Sample: " + '\033[m')
|
|
|
|
# Sample Drop table
|
|
connection.execute("DROP TABLE IF EXISTS sample;")
|
|
# Sample Create
|
|
connection.execute("CREATE TABLE sample(id integer, name text);")
|
|
# Sample Insert
|
|
connection.execute("INSERT INTO sample VALUES (?,?)",("1","test_name"))
|
|
connection.commit()
|
|
# Sample Select
|
|
cursor = connection.execute("SELECT * FROM sample;")
|
|
print(cursor.fetchall())
|
|
|
|
######################################################################
|
|
|
|
class HW2_sql():
|
|
############### DO NOT MODIFY THIS SECTION ###########################
|
|
######################################################################
|
|
def create_connection(self, path):
|
|
connection = None
|
|
try:
|
|
connection = sqlite3.connect(path)
|
|
connection.text_factory = str
|
|
except Error as e:
|
|
print("Error occurred: " + str(e))
|
|
|
|
return connection
|
|
|
|
def execute_query(self, connection, query):
|
|
cursor = connection.cursor()
|
|
try:
|
|
if query == "":
|
|
return "Query Blank"
|
|
else:
|
|
cursor.execute(query)
|
|
connection.commit()
|
|
return "Query executed successfully"
|
|
except Error as e:
|
|
return "Error occurred: " + str(e)
|
|
######################################################################
|
|
######################################################################
|
|
|
|
# GTusername [0 points]
|
|
def GTusername(self):
|
|
gt_username = "tlou31"
|
|
return gt_username
|
|
|
|
# Part a.i Create Tables [2 points]
|
|
def part_ai_1(self,connection):
|
|
############### EDIT SQL STATEMENT ###################################
|
|
part_ai_1_sql = """
|
|
CREATE TABLE IF NOT EXISTS movies (
|
|
id INTEGER ,
|
|
title TEXT,
|
|
score REAL);
|
|
"""
|
|
|
|
######################################################################
|
|
|
|
return self.execute_query(connection, part_ai_1_sql)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
########################### DO NOT MODIFY THIS SECTION ##########################
|
|
#################################################################################
|
|
if SHOW == True:
|
|
sample = Sample()
|
|
sample.sample()
|
|
|
|
print('\033[32m' + "Q2 Output: " + '\033[m')
|
|
db = HW2_sql()
|
|
conn = db.create_connection("Q2")
|
|
|
|
try:
|
|
conn.execute("DROP TABLE IF EXISTS movies;")
|
|
conn.execute("DROP TABLE IF EXISTS movie_cast;")
|
|
conn.execute("DROP TABLE IF EXISTS cast_bio;")
|
|
conn.execute("DROP VIEW IF EXISTS good_collaboration;")
|
|
conn.execute("DROP TABLE IF EXISTS movie_overview;")
|
|
except:
|
|
print("Error in Table Drops")
|
|
|
|
try:
|
|
print('\033[32m' + "part ai 1: " + '\033[m' + str(db.part_ai_1(conn)))
|
|
# print('\033[32m' + "part ai 2: " + '\033[m' + str(db.part_ai_2(conn)))
|
|
except:
|
|
print("Error in Part a.i")
|
|
|
|
conn.commit()
|
|
|
|
print('helloworld') |