create_tables takes as parameters an sql cursor and the name of a file (a string
ID: 3685553 • Letter: C
Question
create_tables takes as parameters an sql cursor and the name of a file (a string). The file contains the database schema, all the necessary create table statements. Each line in the file is a string consisting of a CREATE TABLE statement such as: CREATE TABLE students(student_number VARCHAR(10) PRIMARY KEY, last_name VARCHAR(30), first_name VARCHAR(30), department VARCHAR(40)) using sqllite in python.
The database_schema file contains:
CREATE TABLE students(student_number VARCHAR(10) PRIMARY KEY, last_name VARCHAR(30), first_name VARCHAR(30), department VARCHAR(40))
CREATE TABLE enrolment(student_number VARCHAR(10), course_id VARCHAR(10), term VARCHAR(10), grade INTEGER)
CREATE TABLE faculty(faculty_id VARCHAR(10) PRIMARY KEY, last_name VARCHAR(30), first_name VARCHAR(30), department VARCHAR(40))
CREATE TABLE teaches(instructor_id VARCHAR(10), course_id VARCHAR(10), term VARCHAR(10))
CREATE TABLE courses( course_id VARCHAR(10) PRIMARY KEY, course_name VARCHAR(100), term VARCHAR(10), department VARCHAR(40))
Question:
Complete this function:
import sqlite3
def create_tables(sql_cursor, file_name):
Explanation / Answer
import sqlite3
conn = sqlite3.connect('test.db')
print "Opened database successfully";
conn.execute('''CREATE TABLE STUDENT
(STUDENT-NUMBER CHAR(10) PRIMARY KEY NOT NULL,
FIRST NAME cHAR(30) NOT NULL,
LAST NAME CHAR(30) NOT NULL,
DEPARTMENT CHAR(40);''')
print "STUDENT Table created successfully";
conn.execute('''CREATE TABLE ENROLLMENT
(STUDENT-NUMBER CHAR(10) NOT NULL,
COURSE-ID CHAR(10) NOT NULL,
TERM CHAR(10) NOT NULL,
GRADE INT;''')
print "ENROLLMENT Table created successfully";
conn.execute('''CREATE TABLE FACULTY
(FACULTY-ID CHAR(10) PRIMARY KEY NOT NULL,
FIRST NAME CHAR(30) NOT NULL,
LAST NAME CHAR(30) NOT NULL,
DEPARTMENT CHAR(40);''')
print "FACULTY Table created successfully";
conn.execute('''CREATE TABLE TEACHES
(INSTRUCTOR-ID CHAR(10) NOT NULL,
COURSE-ID CHAR(10) NOT NULL,
TERM CHAR(10);''')
print "TEACHES Table created successfully";
conn.execute('''CREATE TABLE COURSES
(COURSE-ID CHAR(10) PRIMARY KEY NOT NULL,
COURSE NAME cHAR(100) NOT NULL,
TERM CHAR(10) NOT NULL,
DEPARTMENT CHAR(40);''')
print "COURSES Table created successfully";
conn.close()
note-the above code can help to answer the given question.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.