-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakedb.py
More file actions
67 lines (63 loc) · 2.61 KB
/
makedb.py
File metadata and controls
67 lines (63 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 30 15:49:46 2014
@author: ldavis
"""
import csv
import sys
import sqlite3 as lite
from OrganizeData import *
from Professor import *
from parse_course_data import *
from moreParseData import *
#from OCAWEB.sdfinal.models import *
from django.db import models
def makedb():
'''create the database needed for the django website'''
#rip data points from Sarah/Berit's db using Cecelia's code
datapoints = createDataPoints()
#initialize connection to db file
con = lite.connect('oca.db')
#going to need these later
takendict = {}
for point in datapoints:
if point.stuID in takendict:
takendict[point.stuID].append(point.course)
else:
takendict[point.stuID] = [point.course]
stuarchive = []
coursearchive = []
with con:
cur = con.cursor()
#loop through all the datapoints to rip their attributes into a db
dpid = 0
for point in datapoints:
#cur.execute() takes a tuple, so extract course attributes
dpinsertable = (dpid, point.stuID, point.gradYear, point.stuSem, point.stuGen, point.stuMaj, point.course, point.prof)
cur.execute("INSERT INTO Datapoints VALUES(?, ?, ?, ?, ?, ?, ?, ?)", dpinsertable)
dpid += 1
#determine if student is unique, then add to table if so
if point.stuID not in stuarchive:
stuinsertable = (point.stuID, point.gradYear, point.stuGen, point.stuMaj)
cur.execute("INSERT INTO Students VALUES(?, ?, ?, ?)", stuinsertable)
stuarchive.append(point.stuID)
#similarly, add all unique courses to their own table
if point.course not in coursearchive:
courseinsertable = (point.course, courses[point.course])
cur.execute("INSERT INTO Courses VALUES(?, ?)", courseinsertable)
coursearchive.append(point.course)
#the bit of code above more or less defeats the purpose of using django.
#fuck.
#the lines below are better
for dp in Datapoint.objects.all():
student = Student.objects.get(pk=dp.stuid)
course = Course.objects.get(pk=dp.courseid)
student.courses.add(course)
#with should autocommit, but I think I'll be careful
con.commit()
#move, girl, just back it up
data = '\n'.join(con.iterdump())
f = open('oca.sql', 'w')
with f:
f.write(data)
#you can use 'cat oca.sql' from a terminal window to check and see if the database compiled correctly