#!/usr/bin/python # # # 10/10/2016 Alex Barnes # # # This script fixes adds 2 TAGM channels to the translation table. # These new channels correspond to columns 101 and 102. # import sqlite3 as lite import sys import os import re import datetime if len(sys.argv) != 2: print "You must supply a SQLite DB file to modify!" sys.exit(0); # Connect to SQLite DB file db_filename = sys.argv[1] print "" print "opening SQLite DB file: %s" % db_filename print "---------------------------------------------" con = lite.connect(db_filename) with con: # Specify that next cursor should be "dictionary" # (i.e. python's hash map) so columns can be indexed # by name con.row_factory = lite.Row # Create Cursor cur = con.cursor() # Check that this TT file has not already been updated adc_chanid = -1 try: cur.execute("SELECT chanid FROM Channel WHERE name='TAGM-A-102'") (adc_chanid,) = cur.fetchone() except: pass # Do nothing here if adc_chanid >= 0: print 'Fix has already been applied, exiting' sys.exit(-1) cur.execute("INSERT INTO TAGM VALUES(%d,%d,%d,%d,%d)" %(101,0,121,617,489)) cur.execute("INSERT INTO TAGM VALUES(%d,%d,%d,%d,%d)" %(102,0,122,618,490)) ######################################################################### ADCs ######################################################################### #COLUMNS cur.execute("UPDATE Channel SET name='TAGM-A-101' WHERE chanid=121") cur.execute("UPDATE Channel SET name='TAGM-A-102' WHERE chanid=122") ######################################################################### TDCs ######################################################################### #COLUMNS cur.execute("UPDATE Channel SET name='TAGM-T-101' WHERE chanid=617") cur.execute("UPDATE Channel SET name='TAGM-T-102' WHERE chanid=618") ######################################################################## DISCs ######################################################################### #COLUMNS cur.execute("UPDATE Channel SET name='TAGM-T-101' WHERE chanid=489") cur.execute("UPDATE Channel SET name='TAGM-T-102' WHERE chanid=490") ######################################################################################################################################################## # Need to update all entries in the Channel table to fill in # the "system" and "col_name" columns # This code was copied from tt_fix_tagm.py tablename = "TAGM" # Get column name by selecting just the first row cur.execute("SELECT * FROM %s LIMIT 1" % (tablename)) colnames = cur.fetchone().keys() chan_cols = [] for colname in colnames: if "_chanid" in colname: chan_cols.append(colname) # If no rows in table or no columns with names ending in "_chanid" found, continue if len(chan_cols)!= 0: print "Updating Channel table for system: %s ..." % (tablename) cur.execute("SELECT * FROM %s" % (tablename)) myrows = cur.fetchall() N = 0 for myrow in myrows: for col in chan_cols: query = "UPDATE Channel SET system='%s',col_name='%s' WHERE chanid=%d" % (tablename,col,myrow[col]) cur.execute(query) N += 1 print "Updated %d rows" % N print 'Done'