def update_classes_table(filename):
	
	import sqlite3
	from datetime import datetime
	import globalvars
	conn = sqlite3.connect(globalvars.database_path)
	cur = conn.cursor()
	
	# Make some fresh tables using executescript()
	cur.executescript('''
	DROP TABLE IF EXISTS Classes;
	DROP TABLE IF EXISTS Professors;
	DROP TABLE IF EXISTS Sections;
	DROP TABLE IF EXISTS Times;
	DROP TABLE IF EXISTS Sections_Times;
	DROP TABLE IF EXISTS Con_Time_Time;
	DROP TABLE IF EXISTS Pref_Section_Section;
	DROP TABLE IF EXISTS Pref2_Section_Section;
	DROP TABLE IF EXISTS Con_Section_Section;
	DROP TABLE IF EXISTS Year;
	DROP TABLE IF EXISTS Division;
	DROP TABLE IF EXISTS Skill;
	DROP TABLE IF EXISTS Sections_Year;
	DROP TABLE IF EXISTS Sections_Division;
	DROP TABLE IF EXISTS Sections_Skill;
	
	CREATE TABLE Classes (
		ClassID	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		Name	TEXT NOT NULL,
		YearID	INTEGER DEFAULT 0,
		DivisionID	INTEGER DEFAULT 0,
		SkillID	INTEGER DEFAULT 0,
		ShortName	TEXT UNIQUE,
		Worth	REAL
	);
	
	CREATE TABLE Professors (
		ProfessorID	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		Name	TEXT NOT NULL UNIQUE
	);
	
	CREATE TABLE Sections (
		SectionID	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		Name	TEXT NOT NULL,
		ProfessorID	INTEGER NOT NULL,
		ClassID	INTEGER NOT NULL,
		NumberOpen	INTEGER,
		Seats	INTEGER,
		Room	TEXT,
		StudentID INTEGER DEFAULT 0,
		Scheduled INTEGER DEFAULT 0,
		Worth	REAL
	);
	
	CREATE TABLE Times (
		Time	TEXT,
		Start	TEXT,
		End		TEXT,
		Day	TEXT,
		TimeID	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE
	);
	
	CREATE TABLE Sections_Times (
		PrimaryKey	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		SectionID	INTEGER NOT NULL,
		TimeID	INTEGER NOT NULL
	);
	
	CREATE TABLE Con_Time_Time (
		PrimaryKey	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		TimeID1	INTEGER NOT NULL,
		TimeID2	INTEGER NOT NULL
	);
	
	CREATE TABLE Pref_Section_Section (
		PrimaryKey	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		SectionID1	INTEGER NOT NULL,
		SectionID2	INTEGER NOT NULL
	);
	
	CREATE TABLE Pref2_Section_Section (
		PrimaryKey	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		SectionID1	INTEGER NOT NULL,
		SectionID2	INTEGER NOT NULL
	);

	CREATE TABLE Con_Section_Section (
		PrimaryKey	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		SectionID1	INTEGER NOT NULL,
		SectionID2	INTEGER NOT NULL
	);
	
	CREATE TABLE Year (
		PrimaryKey INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		Name	TEXT
	);
	
	CREATE TABLE Division (
		PrimaryKey INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		Name	TEXT
	);
	CREATE TABLE Skill (
		PrimaryKey INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		Name	TEXT
	);
	CREATE TABLE Sections_Year(
		PrimaryKey INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		SectionID	INTEGER NOT NULL,
		YearID	INTEGER NOT NULL
	);
		CREATE TABLE Sections_Division(
		PrimaryKey INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		SectionID	INTEGER NOT NULL,
		DivisionID	INTEGER NOT NULL
	);
		CREATE TABLE Sections_Skill(
		PrimaryKey INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		SectionID	INTEGER NOT NULL,
		SkillID	INTEGER NOT NULL
	)
	
	
	''')
	
	# create YDS
	years = [1,2,3,4,5,"any"]
	skills = [1,2,3,4,5,"any"]
	divisions = ["A", "B", "I", "O", "P", "any"]
	
	for i in range(6):
		cur.execute('INSERT OR IGNORE INTO Year (Name) VALUES(?)', (years[i],))
		cur.execute('INSERT OR IGNORE INTO Division (Name) VALUES(?)', (divisions[i],))
		cur.execute('INSERT OR IGNORE INTO Skill (Name) VALUES(?)', (skills[i],))
	
	
	# update from each row in data/listings.csv
	# as created by update_classes.py
	#filename = 'data/listings.csv'
	for entry in open(filename):
		entry = entry.rstrip()
		entry = entry.split(',')
		# create and get ProfessorID
		if len(entry) == 12:
			professor = entry[10] + ', ' + entry[11]
			professor = professor[1:-1]
			cur.execute('''INSERT OR IGNORE INTO Professors (Name) VALUES(?)''', (professor,))
			cur.execute('SELECT ProfessorID FROM Professors WHERE Name = ?', (professor, ))
			ProfessorID = cur.fetchone()[0]
			conn.commit()
		
		if len(entry) == 11:
			if entry[10] != '\xc2\xa0':
				professor = entry[10].strip()
				cur.execute('''INSERT OR IGNORE INTO Professors (Name) VALUES ( ? )''', (str(professor), ) )
				cur.execute('SELECT ProfessorID FROM Professors WHERE Name = ?', (professor, ))
				ProfessorID = cur.fetchone()[0]
				conn.commit()
			
			if entry[10] == '\xc2\xa0':
				professor = 'Staff'
				cur.execute('''INSERT OR IGNORE INTO Professors (Name)
	        	VALUES ( ? )''', (str(professor), ) )
				cur.execute('SELECT ProfessorID FROM Professors WHERE Name = ?', (professor, ))
				ProfessorID = cur.fetchone()[0]
				conn.commit()
		
		# create and get ClassID
		if len(entry) > 11:
			classname = entry[1]
			classother = entry[0]
			shortname = classother[0:7]
			worth = entry[2]
			
			cur.execute('''INSERT OR IGNORE INTO Classes (Name, ShortName) 
			VALUES ( ?, ? )''', ( classname, shortname ) )
			cur.execute('SELECT ClassID FROM Classes WHERE ShortName = ? ', (shortname, ))
			ClassID = cur.fetchone()[0]
			
			# create and get SectionID
			sectionnum = classother[7:10]
			try:
				cur.execute('SELECT SectionID FROM Sections WHERE Name = ? and ClassID = ?', (sectionnum, ClassID))
				SectionID = cur.fetchone()[0]
			except:
				cur.execute('''INSERT OR IGNORE INTO Sections 
			(Name, ProfessorID, ClassID, Worth) VALUES (?, ?, ?, ?)''', (sectionnum, ProfessorID, ClassID, worth) )
				cur.execute('SELECT SectionID FROM Sections WHERE Name = ? and ClassID = ?', (sectionnum, ClassID))
				SectionID = cur.fetchone()[0]
			
			
			# get YDS
			if entry[3] == 'any':
				#for y in years[:-1]:
				#	cur.execute('SELECT PrimaryKey FROM Year WHERE Name = ?',(y,))
				#	YearID = cur.fetchone()[0]
				cur.execute('UPDATE Classes SET YearID = ? WHERE ClassID = ?',(6, ClassID))
				entry[3] = '12345'
			if entry[3] != 'any':
				for y in entry[3]:
					cur.execute('SELECT PrimaryKey FROM Year WHERE Name = ?',(y,))
					YearID = cur.fetchone()[0]
					cur.execute('UPDATE Classes SET YearID = ? WHERE ClassID = ?',(YearID, ClassID))
			
			if entry[4] == 'any':
				#for y in divisions[:-1]:
				#	print y
				#	cur.execute('SELECT PrimaryKey FROM Division WHERE Name = ?',(y,))
				#	DivisionID = cur.fetchone()[0]
				cur.execute('UPDATE Classes SET DivisionID = ? WHERE ClassID = ?',(6, ClassID))
				entry[4] = 'ABIOP'