FCM82 - Python Programming #52
- def OpenDB():
- global connection
- global cursor
- connection = apsw.Connection("labpackagestudy.db3")
- cursor = connection.cursor()
- def DoWeekDays():
- # Create a second cursor for updating the new table
- cursor2 = connection.cursor()
- q1 = "DROP TABLE IF EXISTS weekdays"
- query = '''CREATE TABLE IF NOT EXISTS weekdays AS SELECT pkgs,
- Count(DOW) as CountOfDOW FROM study WHERE (Holiday <> 1)
- AND DayName in
- GROUP BY pkgs'''
- addcolquery = 'ALTER TABLE weekdays ADD COLUMN probability REAL'
- addcolquery = 'ALTER TABLE weekdays ADD COLUMN lower REAL'
- addcolquery = 'ALTER TABLE weekdays ADD COLUMN upper REAL'
- sumquery = "SELECT Sum(CountOfDOW) as Sm FROM weekdays"
- tmp = cursor.execute(sumquery)
- for t in tmp:
- DaySum = t
- upquery = "SELECT * FROM weekdays"
- c1 = cursor.execute(upquery)
- LastUpper = .0
- for row in c1:
- cod = row
- pkg = row
- prob = cod / float(DaySum)
- if LastUpper != .0:
- lower = LastUpper
- LastUpper = (lower + prob)
- lower = .0
- LastUpper = prob
- nquery = 'UPDATE weekdays SET probability = %f, \
- lower = %f, upper = %f WHERE pkgs = %d' \
- % (prob,lower,LastUpper,pkg)
- u = cursor2.execute(nquery)
- # End of DoWeekDays
RAW Paste Data Copied