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

169 lines
3.9 KiB
Python
Raw Normal View History

2008-05-29 10:15:21 +02:00
#!/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 *
2008-06-04 19:35:09 +02:00
#from PyQt4.QtSql import *
2008-05-29 10:15:21 +02:00
from main import *
from assignment import *
from reading import *
from schedule import *
from calendar import *
2008-06-04 19:35:09 +02:00
from book import *
from course import *
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
class DB:
def initDB():
conn = sqlite.connect('egon.db')
curs = conn.cursor()
return curs, conn
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def initNewDB():
cursor, conn = initDB()
initAssignmentDB(cursor)
initReadingDB(cursor)
initScheduleDB(cursor)
initBookDB(cursor)
initCourseDB(cursor)
exitDB(conn)
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def initAssignmentDB(cursor):
cursor.execute('''
CREATE TABLE Assignment (
aid INTEGER PRIMARY KEY,
date TEXT,
description TEXT,
available BOOLEAN,
begun BOOLEAN,
finished BOOLEAN,
delivered BOOLEAN
)
''')
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def initReadingDB(cursor):
cursor.execute('''
CREATE TABLE Reading (
rid INTEGER PRIMARY KEY,
week TEXT,
chapter TEXT,
pages TEXT
)
''')
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def initScheduleDB(cursor):
cursor.execute('''
CREATE TABLE Lesson (
lid INTEGER PRIMARY KEY,
day TEXT,
fromtime TEXT,
totime TEXT,
type TEXT,
room TEXT
)
''')
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def initBookDB(cursor):
cursor.execute('''
CREATE TABLE Book (
isbn TEXT PRIMARY KEY,
title TEXT,
author TEXT,
edition INTEGER
)
''')
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def initCourseDB(cursor):
cursor.execute('''
CREATE TABLE Course (
code TEXT PRIMARY KEY,
title TEXT,
short TEXT
)
''')
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def initAssignmentInCourse(cursor):
cursor.execute('''
CREATE TABLE assignmentInCourse (
assignment INTEGER,
course INTEGER
)
''')
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def initReadingInCourse(cursor):
cursor.execute('''
CREATE TABLE readingInCourse (
reading INTEGER,
course INTEGER
)
''')
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def initLessonInCourse(cursor):
cursor.execute('''
CREATE TABLE lessonInCourse (
lesson INTEGER,
course INTEGER
)
''')
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def initCourseUsesBook(cursor):
cursor.execute('''
CREATE TABLE courseUsesBook (
course INTEGER,
book TEXT
)
''')
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def addNewBook(isbn, title, author, edition, course):
cursor, conn = initDB()
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
bookQuery = '''
INSERT INTO Book
VALUES (%s, %s, %s, %i)
''' % (isbn, title, author, edition)
courseUsesBookQuery = '''
INSERT INTO courseUsesBook
VALUES (%i, %s)
''' % (course, isbn)
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
cursor.execute(bookQuery)
cursor.execute(courseUsesBookQuery)
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
exitDB(conn)
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
def addNewCourse(code, title, short):
cursor, conn = initDB()
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
courseQuery = '''
INSERT INTO Course
VALUES (%s, %s, %s)
''' % (code, title, short)
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
cursor.execute(courseQuery)
2008-05-29 10:15:21 +02:00
2008-06-05 13:30:13 +02:00
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()
2008-05-29 10:15:21 +02:00
initNewDB()