Advertisement
gregwa

FCM82 - Python Programming #52

Feb 9th, 2014
375
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def OpenDB():
  2.     global connection
  3.     global cursor
  4.     connection = apsw.Connection("labpackagestudy.db3")
  5.     cursor = connection.cursor()
  6.    
  7. def DoWeekDays():
  8.     # Create a second cursor for updating the new table
  9.     cursor2 = connection.cursor()
  10.     q1 = "DROP TABLE IF EXISTS weekdays"
  11.     cursor.execute(q1)
  12.     query = '''CREATE TABLE IF NOT EXISTS weekdays AS SELECT pkgs,
  13.               Count(DOW) as CountOfDOW FROM study WHERE (Holiday <> 1)
  14.               AND DayName in
  15.               ("Monday","Tuesday","Wednesday","Thursday","Friday")
  16.               GROUP BY pkgs'''
  17.  
  18.     cursor.execute(query)
  19.     addcolquery = 'ALTER TABLE weekdays ADD COLUMN probability REAL'
  20.     cursor.execute(addcolquery)
  21.     addcolquery = 'ALTER TABLE weekdays ADD COLUMN lower REAL'
  22.     cursor.execute(addcolquery)
  23.     addcolquery = 'ALTER TABLE weekdays ADD COLUMN upper REAL'
  24.     cursor.execute(addcolquery)
  25.     sumquery = "SELECT Sum(CountOfDOW) as Sm FROM weekdays"
  26.     tmp = cursor.execute(sumquery)
  27.     for t in tmp:
  28.         DaySum = t[0]
  29.     upquery = "SELECT * FROM weekdays"
  30.     c1 = cursor.execute(upquery)
  31.     LastUpper = .0
  32.     for row in c1:
  33.         cod = row[1]
  34.         pkg = row[0]
  35.         prob = cod / float(DaySum)
  36.         if LastUpper != .0:
  37.             lower = LastUpper
  38.             LastUpper = (lower + prob)
  39.         else:
  40.             lower = .0
  41.             LastUpper = prob
  42.         nquery = 'UPDATE weekdays SET probability = %f, \
  43.                   lower = %f, upper = %f WHERE pkgs = %d' \
  44.                   % (prob,lower,LastUpper,pkg)
  45.         u = cursor2.execute(nquery)
  46. #====================================
  47. #       End of DoWeekDays
  48. #====================================
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement