tirilane
/
egon
Archived
1
0
Fork 0
This repository has been archived on 2024-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
egon/db.py

171 lines
3.5 KiB
Python

#!/usr/bin/env python
#coding: utf-8
import os
import platform
import sys
from pysqlite2 import dbapi2 as sqlite
from PyQt4.QtCore import *
from PyQt4.QtGui import *
#from PyQt4.QtSql import *
#from main import *
from assignment import *
from reading import *
from schedule import *
from calendar import *
from book import *
from course import *
def initDB():
conn = sqlite.connect('egon.db')
curs = conn.cursor()
return curs, conn
def initNewDB():
cursor, conn = initDB()
initAssignmentDB(cursor)
initReadingDB(cursor)
initScheduleDB(cursor)
initBookDB(cursor)
initCourseDB(cursor)
initAssignmentInCourse(cursor)
initReadingInCourse(cursor)
initLessonInCourse(cursor)
initCourseUsesBook(cursor)
exitDB(conn)
def initAssignmentDB(cursor):
cursor.execute('''
CREATE TABLE Assignment (
aid INTEGER PRIMARY KEY,
date TEXT,
description TEXT,
available BOOLEAN,
begun BOOLEAN,
finished BOOLEAN,
delivered BOOLEAN
)
''')
def initReadingDB(cursor):
cursor.execute('''
CREATE TABLE Reading (
rid INTEGER PRIMARY KEY,
week TEXT,
chapter TEXT,
pages TEXT
)
''')
def initScheduleDB(cursor):
cursor.execute('''
CREATE TABLE Lesson (
lid INTEGER PRIMARY KEY,
day TEXT,
fromtime TEXT,
totime TEXT,
type TEXT,
room TEXT
)
''')
def initBookDB(cursor):
cursor.execute('''
CREATE TABLE Book (
isbn TEXT PRIMARY KEY,
title TEXT,
author TEXT,
edition INTEGER
)
''')
def initCourseDB(cursor):
cursor.execute('''
CREATE TABLE Course (
code TEXT PRIMARY KEY,
title TEXT,
short TEXT
)
''')
def initAssignmentInCourse(cursor):
cursor.execute('''
CREATE TABLE assignmentInCourse (
assignment INTEGER,
course INTEGER
)
''')
def initReadingInCourse(cursor):
cursor.execute('''
CREATE TABLE readingInCourse (
reading INTEGER,
course INTEGER
)
''')
def initLessonInCourse(cursor):
cursor.execute('''
CREATE TABLE lessonInCourse (
lesson INTEGER,
course INTEGER
)
''')
def initCourseUsesBook(cursor):
cursor.execute('''
CREATE TABLE courseUsesBook (
course INTEGER,
book TEXT
)
''')
def addNewBook(isbn, title, author, edition, course):
cursor, conn = initDB()
bookQuery = '''
INSERT INTO Book
VALUES (%s, %s, %s, %i)
''' % (isbn, title, author, edition)
courseUsesBookQuery = '''
INSERT INTO courseUsesBook
VALUES (%i, %s)
''' % (course, isbn)
cursor.execute(bookQuery)
cursor.execute(courseUsesBookQuery)
exitDB(conn)
def addNewCourse(code, title, short):
cursor, conn = initDB()
courseQuery = '''
INSERT INTO Course
VALUES (%s, %s, %s)
''' % (code, title, short)
cursor.execute(courseQuery)
exitDB(conn)
def getCourses():
cursor, conn = initDB()
courseQuery = '''
SELECT *
FROM Course
'''
cursor.execute(courseQuery)
courses = []
for row in cursor.fetchall():
courses.append(CourseModel(row[0], row[1], row[2]))
def exitDB(conn):
conn.commit()
conn.close()
#initNewDB()