def connect(db, sql): import Sybase db = Sybase.connect(hostname, username, password, db) c = db.cursor() c.execute(sql) return c.fetchall()
def open(self): """ Opens a connection to Database server. """ super(sql_connector, self).open() connector = False if self.con_type == 'postgres': import psycopg2 import psycopg2.extensions connector = psycopg2.connect("dbname=%s user=%s host=%s port=%s password=%s sslmode=%s" \ % (self.db, self.uid, self.host, self.port, self.passwd, self.sslmode)) connector.set_isolation_level( psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) elif self.con_type == 'mysql': import MySQLdb connector = MySQLdb.Connection(db=self.db, host=self.host, port=self.port, user=self.uid, passwd=self.pwd) elif self.con_type == 'oracle': import cx_Oracle dsn_tns = cx_Oracle.makedsn(self.host, self.port, self.db) connector = cx_Oracle.connect(self.uid, self.passwd, dsn_tns) elif self.con_type == 'sybase': # to be test import Sybase db = Sybase.connect(self.host, self.uid, self.passwd) db.execute('use %s' % (self.db)) else: raise Exception('Not Supported') return connector
def bsybase(searchhost): database_hn = 'PROD' database_un = 'username' database_pw = 'password' database_db = 'inventory' db = Sybase.connect(database_hn, database_un, database_pw, database_db) c = db.cursor() c.execute( 'select dev.hostname, dev.device_type, dep.name, tu.first_name, tu.last_name, tu.smtp_address, dev.notes from inventory..device as dev, common.dbo.department as dep, common.dbo.BLAH_user as tu' ### DEPT MUST BE DEFINED ' where dev.BLAH_user_id = tu.BLAH_user_id and tu.department_id = dep.department_id and dev.is_active = "1"' ' and lower(dev.hostname) = @sh', {'@sh': searchhost}) ### SPECIFIC #' and lower(dev.hostname) = "admin1-location" ') #print string.join([row[0] for row in c.fetchall()], '') rows = c.fetchall() if rows: for row in rows: inv_host = row[0] inv_team = row[2] inv_env = "not def in db" inv_os = row[1] inv_osver = "not def in db" inv_owner = row[3], row[4] else: print searchhost + " not in sybase inventory check dept and owner, did you add to inventory after kickstarting or jumpstarting?\n" syslog.syslog( syslog.LOG_ERR, searchhost + " not in sybase inventory check dept and owner, did you add to inventory after kickstarting or jumpstarting?\n" )
def open(self): """ Opens a connection to Database server. """ super(sql_connector, self).open() connector = False if self.con_type == 'postgres': import psycopg2 import psycopg2.extensions connector = psycopg2.connect("dbname=%s user=%s host=%s port=%s password=%s sslmode=%s" \ % (self.db, self.uid, self.host, self.port, self.passwd, self.sslmode)) connector.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) elif self.con_type == 'mysql': import MySQLdb connector = MySQLdb.Connection(db=self.db, host=self.host, port=self.port, user=self.uid, passwd=self.pwd) elif self.con_type == 'oracle': import cx_Oracle dsn_tns = cx_Oracle.makedsn(self.host, self.port, self.db) connector = cx_Oracle.connect(self.uid, self.passwd, dsn_tns) elif self.con_type == 'sybase':# to be test import Sybase db = Sybase.connect(self.host, self.uid, self.passwd) db.execute('use %s' % (self.db)) else: raise Exception('Not Supported') return connector
def handle(self, *args, **kwargs): pprint print 'Connecting...' try: db = Sybase.connect('143.248.5.170:4000', 'otl_academics', 'whgdmstltmxpa!', 'scholar') except Sybase.DatabaseError: print 'Connection failed!' return print 'OK.' cur = db.cursor() while True: try: command = raw_input('>> ') except (EOFError, KeyboardInterrupt): print '' break try: cur.execute(command) rows = cur.fetchall() pprint2(rows) except Exception, e: if e.message == 'No result set': print 'OK, but no result set.' else: print str(e)
def fetch_taking_courses(student_id): """ 학사 DB에 접속하여 해당 학번의 학생이 듣고 있는 과목 정보를 가져온다. """ if not isinstance(student_id, int): raise TypeError('student_id must be an integer.') # TODO: cache!! taking_courses = [] try: import Sybase scholar_db = Sybase.connect(settings.SCHOLARDB_HOST, settings.SCHOLARDB_USER, settings.SCHOLARDB_PASSWORD, settings.SCHOLARDB_NAME) except ImportError: raise DatabaseError('Sybase module is not installed!') except: raise DatabaseError('Cannot access the scholar database!') cursor = scholar_db.cursor() cursor.execute("""SELECT l.subject_no, l.old_no, l.dept_id, l.lecture_class FROM view_OTL_attend a, view_OTL_lecture l WHERE a.student_no = %d AND a.lecture_year = %d AND a.lecture_term = %d AND a.lecture_year = l.lecture_year AND a.lecture_term = l.lecture_term AND a.subject_no = l.subject_no AND a.lecture_class = l.lecture_class AND a.dept_id = l.dept_id""" % (student_id, settings.CURRENT_YEAR, settings.CURRENT_SEMESTER)) rows = cursor.fetchall() try: for row in rows: taking_courses.append(Lecture.objects.get(year=settings.CURRENT_YEAR, semester=settings.CURRENT_SEMESTER, code=row[0], department__id=row[2], class_no=row[3].strip())) except Lecture.DoesNotExist: raise DatabaseError('Matching lecture(%s) which a student(%d) is taking was not found! Check scholar-db synchronization.' % (row[0], student_id)) cursor.close() scholar_db.close() return taking_courses
def _getCursor(self): """ 获取postgresql数据库的游标 """ cursor = None try: db = Sybase.connect(self._host, self._user, self._pass, self._name) cursor = db.cursor() except: raise # 原封不动的把异常抛出到上层调用代码 return cursor
def __init__(self, fake=False, *args, **kw): self.region = os.environ.get("SYS_REGION") or None if self.region == "eu": self.dsn = "LNP_DSDB11" elif self.region == "hk": self.dsn = "HKP_DSDB11" elif self.region == "tk": self.dsn = "TKP_DSDB11" else: self.dsn = "NYP_DSDB11" assert self.dsn, "Cannot determine DSDB instance to use for region %s" % self.region self.syb = Sybase.connect(self.dsn, "dsdb_guest", "dsdb_guest", "dsdb", auto_commit="0") assert self.syb._is_connected, "Cannot connect to %s" % self.dsn
def fetchSybaseData(self): """ Function to fetch data from Sybase database. Fetches all data from the database given in the class constructor. Returns the fetched data as an array. """ import Sybase as S #creates the connection try: DB = S.connect(self.address, self.user, self.password, self.database) except S.Error, e: print '\nError while connecting to the Sybase database %s at %s' % (self.database, self.address) print 'Error %s' % e.args print 'Will exit with status -99...' sys.exit(-99)
def fetchSybaseData(self): """ Function to fetch data from Sybase database. Fetches all data from the database given in the class constructor. Returns the fetched data as an array. """ import Sybase as S #creates the connection try: DB = S.connect(self.address, self.user, self.password, self.database) except S.Error, e: print '\nError while connecting to the Sybase database %s at %s' % ( self.database, self.address) print 'Error %s' % e.args print 'Will exit with status -99...' sys.exit(-99)
def __init__(self, fake=False, *args, **kw): self.region = (os.environ.get('SYS_REGION') or None) if self.region == 'eu': self.dsn = 'LNP_DSDB11' elif self.region == 'hk': self.dsn = 'HKP_DSDB11' elif self.region == 'tk': self.dsn = 'TKP_DSDB11' else: self.dsn = 'NYP_DSDB11' assert self.dsn, "Cannot determine DSDB instance to use for region %s" % self.region self.syb = Sybase.connect(self.dsn, 'dsdb_guest', 'dsdb_guest', 'dsdb', auto_commit='0') assert self.syb._is_connected, "Cannot connect to %s" % self.dsn
def fetch_taking_courses(student_id): """ 학사 DB에 접속하여 해당 학번의 학생이 듣고 있는 과목 정보를 가져온다. """ if not isinstance(student_id, int): raise TypeError('student_id must be an integer.') # TODO: cache!! taking_courses = [] try: import Sybase scholar_db = Sybase.connect(settings.SCHOLARDB_HOST, settings.SCHOLARDB_USER, settings.SCHOLARDB_PASSWORD, settings.SCHOLARDB_NAME) except ImportError: raise DatabaseError('Sybase module is not installed!') except: raise DatabaseError('Cannot access the scholar database!') cursor = scholar_db.cursor() cursor.execute( """SELECT l.subject_no, l.old_no, l.dept_id, l.lecture_class FROM view_OTL_attend a, view_OTL_lecture l WHERE a.student_no = %d AND a.lecture_year = %d AND a.lecture_term = %d AND a.lecture_year = l.lecture_year AND a.lecture_term = l.lecture_term AND a.subject_no = l.subject_no AND a.lecture_class = l.lecture_class AND a.dept_id = l.dept_id""" % (student_id, settings.CURRENT_YEAR, settings.CURRENT_SEMESTER)) rows = cursor.fetchall() try: for row in rows: taking_courses.append( Lecture.objects.get(year=settings.CURRENT_YEAR, semester=settings.CURRENT_SEMESTER, code=row[0], department__id=row[2], class_no=row[3].strip())) except Lecture.DoesNotExist: raise DatabaseError( 'Matching lecture(%s) which a student(%d) is taking was not found! Check scholar-db synchronization.' % (row[0], student_id)) cursor.close() scholar_db.close() return taking_courses
def _get_db_connect(dbSystem, db, user, password): """Create a connection to the database specified on the command line """ if dbSystem == 'SYBASE': import Sybase try: dbh = Sybase.connect(dbSystem, user, password, database=db) except: dbh = None elif dbSystem == 'MYSQL': import MySQLdb try: dbh = MySQLdb.connect(user=user, passwd=password, db=db, host='gimli') except: dbh = None return dbh
def getCursor(server): """ @param server: the name of a server @return: a cursor pointing to the server """ parser = xml.sax.make_parser() handler = db_handler() parser.setContentHandler(handler) parser.parse(db_connection_file) #print handler.mapping #print server.upper() #print handler.mapping[server.upper()][2] #print handler.mapping[server.upper()][3] #print handler.mapping[server.upper()][1] db = Sybase.connect(server.upper(), handler.mapping[server.upper()][2], handler.mapping[server.upper()][3], handler.mapping[server.upper()][1]) curs = db.cursor() return curs
def getOneValue(sql): # logger.info("getOneValue") data = pkgutil.get_data(__package__, 'database.dat') values = re.split("\W+", data) try: db = Sybase.connect(values[0], values[1], values[2], values[3]) c = db.cursor() print("getOneValue. SQL: " + sql) c.execute(sql) data = c.fetchall() c.close() db.close() return data[0][0] # first row, first column #return (string.join([row[0] for row in c.fetchall()], '\n')) except (SystemExit, KeyboardInterrupt): raise except Exception: logger.error('getOneValue Failed', exc_info=False) return ''
def executeSql(sql): data = pkgutil.get_data(__package__, 'database.dat') values = re.split("\W+", data) try: db = Sybase.connect(values[0], values[1], values[2], values[3]) c = db.cursor() print("executeSql. SQL: " + sql) c.execute(sql) c.execute("select @@rowcount") count = c.fetchall()[0][0] c.close() db.close() # return c.rowcount return count # return 1 except (SystemExit, KeyboardInterrupt): raise except Exception: logger.error('executeSql Failed', exc_info=False) return 0
def _get_db_connect(dbSystem,db,user,password): """Create a connection to the database specified on the command line """ if dbSystem=='SYBASE': import Sybase try: dbh = Sybase.connect(dbSystem, user, password, database=db ) except: dbh=None elif dbSystem=='MYSQL': import MySQLdb try: dbh = MySQLdb.connect(user=user, passwd=password, db=db, host='localhost') except: dbh=None return dbh
def connect(self, server): try: svr = None principal = None SERVER = server for line in open('//ms/dist/syb/dba/files/sgp.dat', "r"): svr, principal = line.split(' ') if svr == SERVER: break principal = principal.strip() db = Sybase.connect(SERVER, '', '', delay_connect=1, datetime="auto", auto_commit=1) db.set_property(Sybase.CS_SEC_NETWORKAUTH, Sybase.CS_TRUE) db.set_property(Sybase.CS_SEC_SERVERPRINCIPAL, principal) db.connect() dbCursor = db.cursor() return dbCursor except Exception as e: self.logger.error('Error in database connection %s', e) print 'Error in database connection ', e
#! /usr/bin/env python import Sybase # Connexion a la base "pubs2". 'TCOSERVER' est l'adresse du serveur telle que defini dans "/opt/sybase/interfaces" conn = Sybase.connect('TCOSERVER', 'tarek1', '123456', 'pubs2') # Creer un curseur cursor = conn.cursor() # Executer une requete cursor.execute("select * from titles where price > 15.00") # Recuperer et afficher le resultat rows = cursor.fetchall() for row in rows: print row
default=1.0, help="How many sigma from the prediction are you willing to look?") (opt, args) = parser.parse_args() if not opt.date or not opt.abg: parser.print_help() parser.error( "You must give both an ABG file and a DATE to search for observations on\n" ) sys.exit(0) import sys, RO.StringUtil if opt.verbose: sys.stderr.write("Opening a connection to SYBASE\n") db = Sybase.connect('SYBASE', user_name, user_passwd, database=db_name) obj = predict(opt.abg, opt.date) import string date = string.replace(opt.date, ' ', '/') dbcmd = """ SELECT distinct(r.expnum) FROM wcsInfo w JOIN cfht_received r ON w.dataset_name=r.dataset_name WHERE w.htm_index like '%s%s' ORDER BY expnum """ % (htmIndex(obj['RA'], obj['DEC'], htm_level=5), '%') if opt.verbose: sys.stderr.write( "Searching Archive for images at correct location using command\n %s\n" % (dbcmd, ))
def find_images(cdata): import os intersect_info = {} ################################################# # get datasets that contain cutout circle center ################################################# db = Sybase.connect('SYBASE', user_name, user_passwd, database=db_name) dbcmd = """ SELECT w.file_id,w.ext_no,w.* FROM wcsInfo w JOIN cfht_received r ON w.dataset_name=r.dataset_name JOIN exposure e ON e.expnum=r.expnum WHERE w.dataset_name LIKE '%s' AND w.htm_index LIKE '%s%s' AND abs(night-%s) < 1 ORDER BY w.file_id """ % (cdata['dataset_name'], cdata['htm_index'], '%', cdata['night']) c = db.cursor() c.execute(dbcmd) wcsInfo = c.fetchall() import wcsutil intersect_dataset = {} intersect_extnum = {} intersect_cutout = {} import string largest_area_value = -1 largest_area_dataset = '' for dataset in wcsInfo: ### build a WCSObject wcsDict = {} for i, elt in enumerate(dataset): wcsDict[string.upper(c.description[i][0])] = elt wcs = wcsutil.WCSObject(wcsDict) (cutout, area) = circTOcutout(wcs, cdata['RA'], cdata['DEC'], max(0.015, cdata['RADIUS'])) key = str(dataset[0]) + string.zfill(int(dataset[1]), 2) if cutout == None: continue if area > largest_area_value: largest_area_value = area largest_area_dataset = key intersect_cutout[key] = cutout intersect_dataset[key] = str(dataset[0]) intersect_extnum[key] = str(dataset[1]) c.close() db.close() ############################################################## # construct dictionary of nested dictionaries to be returned ############################################################## intersect_info['dataset'] = intersect_dataset intersect_info['extnum'] = intersect_extnum intersect_info['cutout'] = intersect_cutout data_ok = -1 if len(intersect_dataset) > 0: data_ok = 1 return largest_area_dataset, intersect_info, data_ok
def __init__(self, username='******', password=None, server='SYB_JAC', database='omp'): if password is None: password = getpass('Database password for ' + username + ': ') self.db = Sybase.connect(server, username, password, database=database)
def connect_to_database (server, user, password, db): dbproc = Sybase.connect (server, user, password, db, auto_commit = 1 ) return (dbproc)
def __init__(self, p_db): db = p_db s = "" self.db = None self.cursor = None (self.dbtype, driver, hostname, port, dbname, user, pwd) = getDbStringDetails(db) if (self.dbtype in ["teradata", "maxdb"]) or (driver == "-odbc"): dsn = self.odbc(hostname, port, dbname) print dsn self.db = pyodbc.connect(dsn=dsn, user=user, password=pwd, ansi=True, autocommit=True) self.cursor=self.db.cursor() elif self.dbtype == "ase": # hostname defined in interface file self.db = Sybase.connect(dsn=hostname, user=user, passwd=pwd, database=dbname, auto_commit=True) self.cursor = self.db.cursor() self.cursor.execute("set quoted_identifier on") elif self.dbtype in ["asa", "iq"]: import sqlanydb # Module for Sybase ASA or IQ s = "%s" % (hostname) print s self.db = sqlanydb.connect(eng=s, userid=user, password=pwd, dbn=dbname) self.cursor = self.db.cursor() elif self.dbtype == "mssql": s = "%s:%s" % (hostname, port) self.db = pymssql.connect(host=s, user=user, password=pwd, database=dbname, as_dict=False) self.cursor = self.db.cursor() elif self.dbtype == "mysql": self.db = MySQLdb.connect (host=hostname, port=int(port), user = user, passwd = pwd, db=dbname) self.cursor = self.db.cursor() elif self.dbtype == "db2": self.db = DB2.connect (dsn=dbname, uid=user, pwd=pwd) self.cursor = self.db.cursor() elif self.dbtype in ["postgres", "greenplum"]: s = "host='%s' port='%s' user='******' password='******' dbname='%s'" % (hostname, port, user, pwd, dbname) self.db = psycopg2.connect (s) self.db.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) self.cursor = self.db.cursor() elif self.dbtype == "oracle": s = "%s/%s@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%s))(CONNECT_DATA=(SERVICE_NAME=%s)))" s = s % (user, pwd, hostname, port, dbname) self.db = cx_Oracle.connect(s) self.cursor = self.db.cursor() elif self.dbtype == "netezza": # conn="DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; PORT=3306; DATABASE=mysql; UID=joe; # PASSWORD=bloggs; OPTION=3;SOCKET=/var/run/mysqld/mysqld.sock;" self.cursor = Connect (hostname, user, pwd) elif self.dbtype in ["hana"]: from hdbcli import dbapi self.db = dbapi.connect(address=hostname, port=30015+int(port), user=user, password=pwd, autocommit=True) self.cursor = self.db.cursor() elif self.dbtype in ["progress"]: dsn = self.odbc(hostname, port, dbname) self.db = pyodbc.connect(dsn=dsn, user=user, password=pwd, autocommit=True) self.cursor=self.db.cursor() elif self.dbtype in ["ingres", "vectorwise"]: # vnode = @host,protocol,port[;attribute=value{;attribute=value}][[user,password]] s = "@%s,tcp_ip,%s;connection_type=direct" % (hostname, port) self.db=ingresdbi.connect(database=dbname, vnode=s, uid=user, pwd=pwd, dbms_pwd=pwd, autocommit = "Y") # trace = (7, "dbi.log") self.cursor=self.db.cursor() else: perror("Unknown_db_type", self.dbtype)
import psycopg2 import redis import Sybase pgcon = psycopg2.connect(database="hbdp", user="******", password="******", host="198.218.6.169", port="5432") c = pgcon.cursor() sql = "select * from hb10_reserve_list_brief" c.execute(sql) rows = c.fetchall() for i in rows: print i c.close() pgcon.close() r = redis.Redis(host='198.218.6.165', port=10001, decode_responses=True) print(r.get('p_20171130_G1_M_')) print(r.get('honour_20171201#G115#M#')) sybcon = Sybase.connect('A2_DS', 'sa', '', 'center') syb_c = sybcon.cursor() syb_sql = "SELECT reserve_no,* FROM HB10_reserve_list where login_name='yupeng'" syb_c.execute(syb_sql) syb_rows = syb_c.fetchall() for j in syb_rows: print j syb_c.close() sybcon.close()
def handle(self, *args, **options): rx_dept_code = re.compile(ur'([a-zA-Z]+)(\d+)') host = options.get('host', None) user = options.get('user', None) password = options.get('password', None) encoding = options.get('encoding', 'cp949') exclude_lecture = options.get('exclude_lecture', False) lecture_count = 0 try: if password is None: password = getpass.getpass() except (KeyboardInterrupt, EOFError): print return try: db = Sybase.connect(host, user, password, 'scholar') except Sybase.DatabaseError: print >> sys.stderr, 'Connection failed. Check username and password.' return c = db.cursor() if not exclude_lecture: c.execute( 'SELECT * FROM view_OTL_charge WHERE lecture_year = %d AND lecture_term = %d' % (settings.NEXT_YEAR, settings.NEXT_SEMESTER)) professors = c.fetchall() c.execute( 'SELECT * FROM view_OTL_lecture WHERE lecture_year = %d AND lecture_term = %d ORDER BY dept_id' % (settings.NEXT_YEAR, settings.NEXT_SEMESTER)) rows = c.fetchall() departments = {} lectures_not_updated = set() for lecture in Lecture.objects.filter( year=settings.NEXT_YEAR, semester=settings.NEXT_SEMESTER): lectures_not_updated.add(( lecture.code, lecture.year, lecture.semester, lecture.department.id, lecture.class_no, )) # Make Staff Professor with ID 830 try: staff_professor = Professor.objects.get(professor_id=830) except Professor.DoesNotExist: staff_professor = Professor.objects.create(professor_id=830) staff_professor.professor_name = 'Staff' staff_professor.professor_name_en = 'Staff' staff_professor.save() prev_department = None for row in rows: myrow = [] for elem in row: if isinstance(elem, str): try: elem = elem.decode(encoding) except UnicodeDecodeError: eleme = u'%s (???)' % row[20] print >> sys.stderr, 'ERROR: parsing error on lecture %s' % row[ 20] print >> sys.stderr, ' cannot read "%s" in cp949.' % elem myrow.append(elem) # Extract department info. lecture_no = myrow[2] lecture_code = myrow[20] lecture_class_no = myrow[3].strip() department_no = lecture_no[0:2] department_id = int(myrow[4]) department_code = rx_dept_code.match(lecture_code).group(1) # Update department info. if prev_department != department_id: try: department = Department.objects.get(id=department_id) print 'Updating department: %s' % department except Department.DoesNotExist: department = Department(id=department_id) print 'Adding department: %s(%d)...' % ( department_code, department_id) department.num_id = department_no department.code = department_code department.name = myrow[5] department.name_en = myrow[6] department.save() prev_department = department_id # Extract lecture info. #try: #print 'Retreiving %s: %s [%s]...' % (lecture_code, myrow[7].encode('utf-8'), lecture_class_no) #except UnicodeDecodeError: #print 'Retreiving %s: ??? [%s]...' % (lecture_code, lecture_class_no) #myrow[7] = u'???' lecture_key = { 'code': lecture_no, 'year': int(myrow[0]), 'semester': int(myrow[1]), 'department': Department.objects.get(id=department_id), 'class_no': lecture_class_no, } # Convert the key to a hashable object (tuple). lecture_key_hashable = ( lecture_no, int(myrow[0]), int(myrow[1]), department_id, lecture_class_no, ) try: lecture = Lecture.objects.get(**lecture_key) print 'Updating existing lecture...' except Lecture.DoesNotExist: lecture = Lecture(**lecture_key) lecture.num_people = 0 print 'Creating new lecture...' # Update lecture info. lecture.old_code = myrow[20] lecture.title = myrow[7] lecture.title_en = myrow[8] lecture.type = myrow[10] # 과목구분 (한글) lecture.type_en = myrow[11] # 과목구분 (영문) lecture.audience = int(myrow[12]) # 학년 구분 lecture.limit = myrow[17] # 인원제한 lecture.credit = myrow[16] # 학점 lecture.credit_au = myrow[13] # AU lecture.num_classes = int(myrow[14]) # 강의시수 lecture.num_labs = int(myrow[15]) # 실험시수 lecture.notice = myrow[19] # 비고 lecture.is_english = True if myrow[ 21] == 'Y' else False # 영어강의 여부 lecture.deleted = False # Course save try: course = Course.objects.get(old_code=lecture.old_code) course.department = department course.type = lecture.type course.type_en = lecture.type_en course.title = lecture.title.split("<")[0].split("[")[0] course.title_en = lecture.title_en.split("<")[0].split( "[")[0] course.save() # print "Updating Course ... %s" % course.title except Course.DoesNotExist: course = Course() course.old_code = lecture.old_code course.department = department course.type = lecture.type course.type_en = lecture.type_en course.title = lecture.title.split("<")[0].split("[")[0] course.title_en = lecture.title_en.split("<")[0].split( "[")[0] course.score_average = 0 course.load_average = 0 course.gain_average = 0 course.save() # print "Making new Course ... %s" % course.title lecture.course = course lecture.save() lecture_count += 1 # professor save match_scholar = filter( lambda a: lecture.year == a[0] and lecture.semester == a[ 1] and lecture.code == a[2] and lecture.class_no.strip( ) == a[3].strip() and lecture.department_id == a[4], professors) if len(match_scholar) != 0: professors_not_updated = set() for prof in lecture.professor.all(): professors_not_updated.add(prof.id) for i in match_scholar: try: prof_id = i[5] prof_name = unicode(i[6], 'cp949') if i[8] is None or i[8] == '': prof_name_en = '' else: prof_name_en = unicode(i[8].strip(), 'cp949') professor = Professor.objects.get( professor_id=prof_id) if professor.professor_name != prof_name and prof_id != 830: professor.professor_name = prof_name professor.save() if professor.professor_name_en != prof_name_en and prof_id != 830 and prof_name_en != '': professor.professor_name_en = prof_name_en professor.save() professors_not_updated.remove(professor.id) except Professor.DoesNotExist: professor = Professor.objects.create( professor_id=prof_id) professor.professor_name = prof_name professor.professor_name_en = prof_name_en professor.save() # print "Making new Professor ... %s" % professor.professor_name except KeyError: pass lecture.professor.add(professor) if professor.professor_id != 830: lecture.course.professors.add(professor) else: lecture.professor.add(staff_professor) for key in professors_not_updated: professor = Professor.objects.get(id=key) lecture.professor.remove(professor) try: lectures_not_updated.remove(lecture_key_hashable) except KeyError: pass c.close() # Extract exam-time, class-time info. print 'Extracting exam time information...' c = db.cursor() c.execute( 'SELECT * FROM view_OTL_exam_time WHERE lecture_year = %d AND lecture_term = %d' % (settings.NEXT_YEAR, settings.NEXT_SEMESTER)) exam_times = c.fetchall() c.close() ExamTime.objects.filter( lecture__year__exact=settings.NEXT_YEAR, lecture__semester=settings.NEXT_SEMESTER).delete() for row in exam_times: myrow = [] for elem in row: if isinstance(elem, str): elem = elem.decode(encoding) myrow.append(elem) lecture_key = { 'code': myrow[2], 'year': int(myrow[0]), 'semester': int(myrow[1]), 'department': Department.objects.filter(id=int(myrow[4]))[0], 'class_no': myrow[3].strip(), } try: lecture = Lecture.objects.get(**lecture_key) exam_time = ExamTime(lecture=lecture) exam_time.day = int(myrow[5]) - 1 exam_time.begin = time(hour=myrow[6].hour, minute=myrow[6].minute) exam_time.end = time(hour=myrow[7].hour, minute=myrow[7].minute) print 'Updating exam time for %s' % lecture exam_time.save() except Lecture.DoesNotExist: print 'Exam-time for non-existing lecture %s; skip it...' % myrow[ 2] print 'Extracting class time information...' c = db.cursor() c.execute( 'SELECT * FROM view_OTL_time WHERE lecture_year = %d AND lecture_term = %d' % (settings.NEXT_YEAR, settings.NEXT_SEMESTER)) class_times = c.fetchall() c.close() ClassTime.objects.filter( lecture__year__exact=settings.NEXT_YEAR, lecture__semester=settings.NEXT_SEMESTER).delete() for row in class_times: myrow = [] for elem in row: if isinstance(elem, str): elem = elem.decode(encoding) myrow.append(elem) lecture_key = { 'code': myrow[2], 'year': int(myrow[0]), 'semester': int(myrow[1]), 'department': Department.objects.filter(id=int(myrow[4]))[0], 'class_no': myrow[3].strip(), } try: lecture = Lecture.objects.get(**lecture_key) class_time = ClassTime(lecture=lecture) class_time.day = int(myrow[5]) - 1 class_time.begin = time(hour=myrow[6].hour, minute=myrow[6].minute) class_time.end = time(hour=myrow[7].hour, minute=myrow[7].minute) class_time.type = myrow[8] class_time.building = myrow[9] class_time.room = myrow[10] class_time.room_ko = myrow[12] class_time.room_en = myrow[13] try: class_time.unit_time = int(myrow[11]) except (ValueError, TypeError): class_time.unit_time = 0 print 'Updating class time for %s' % lecture class_time.save() except Lecture.DoesNotExist: print 'Class-time for non-existing lecture %s; skip it...' % myrow[ 2] ''' c = db.cursor() c.execute('SELECT * FROM view_OTL_syllabus WHERE lecture_year = %d AND lecture_term = %d' % (settings.NEXT_YEAR, settings.NEXT_SEMESTER)) syllabuses = c.fetchall() c.close() Syllabus.objects.filter(lecture__year__exact=settings.NEXT_YEAR, lecture__semester=settings.NEXT_SEMESTER).delete() for row in syllabuses: myrow = [] for elem in row: if isinstance(elem, str): try: elem = elem.decode(encoding) except UnicodeDecodeError: eleme = u'%s (???)' % row[2] print>>sys.stderr, 'ERROR: parsing error on lecture %s' % row[2] print>>sys.stderr, ' cannot read "%s" in cp949.' % elem myrow.append(elem) lecture_key = { 'code': myrow[2], 'year': int(myrow[0]), 'semester': int(myrow[1]), 'department': Department.objects.filter(id = int(myrow[4]))[0], 'class_no': myrow[3].strip(), } try: lecture = Lecture.objects.get(**lecture_key) syllabus = Syllabus(lecture=lecture) syllabus.professor_info = myrow[5] syllabus.abstract = myrow[6] syllabus.evluation = myrow[7] syllabus.materials = myrow[8] syllabus.plan = myrow[9] syllabus.etc = myrow[10] syllabus.url = myrow[11] syllabus.attachment = myrow[12] print 'Updating syllabus information for %s' % lecture syllabus.save() except Lecture.DoesNotExist: print 'Syllabus information for non-existing lecture %s; skip it...' % myrow[2] ''' if not exclude_lecture: # Mark deleted lectures to notify users. print 'Marking deleted lectures...' for key in lectures_not_updated: lecture_key = { 'code': key[0], 'year': key[1], 'semester': key[2], 'department': Department.objects.get(id=key[3]), 'class_no': key[4], } lecture = Lecture.objects.get(**lecture_key) lecture.deleted = True # print '%s is marked as deleted...' % lecture lecture.save() db.close() print '\nTotal number of departments : %d' % Department.objects.count() print 'Total number of lectures newly added : %d' % lecture_count
utc_val = ny_val.astimezone(pytz.utc) return "'%s'" % utc_val.isoformat(" ") if val is None: return "NULL" else: return repr(val) values = ", ".join(map(show, row)) query = "INSERT INTO rhicFileHeaderV (%s) VALUES (%s);" % (fields, values) fp.write("%s\n" % query) print " [*] Copying SDDS files ..." with tempfile.NamedTemporaryFile(delete=True) as filelist: for row in res: remote_path = row[field_list.index("filePath")] # Check remote path prefix CAD_SDDS_BASE assert(os.path.commonprefix([remote_path, CAD_SDDS_BASE]) == CAD_SDDS_BASE) local_path = remote_path[len(CAD_SDDS_BASE):] filelist.write("%s\n" % local_path) filelist.flush() subprocess.call(["rsync", "-ptgo", "--files-from=%s" % filelist.name, "%s:%s" % (CAD_HOST, CAD_SDDS_BASE), OUTPUT_DIR]) if __name__ == '__main__': print " [*] Connecting to the database ..." conn = Sybase.connect("localhost:5000", "harmless", "harmless", "run_fy13_fill", outputmap = Sybase.DateTimeAsPython) fetch(conn, "RHIC/Polarimeter/Yellow/biasReadbacks") fetch(conn, "RHIC/Polarimeter/Blue/biasReadbacks") fetch(conn, "RHIC/BeamIons") fetch(conn, "RHIC/Rf/Voltage_Monitor_StripChart") fetch(conn, "RHIC/PowerSupplies/rot-ps") fetch(conn, "RHIC/PowerSupplies/snake-ps")
sum(case when datediff(mm,getdate(),tdate)=-5 and trans_type in ('D','I') then d.total else 0 end) as month1, sum(case when datediff(mm,getdate(),tdate)=-4 and trans_type in ('D','I') then d.total else 0 end) as month2, sum(case when datediff(mm,getdate(),tdate)=-3 and trans_type in ('D','I') then d.total else 0 end) as month3 from %s as d LEFT JOIN DefectionNotices as n on d.card_no = n.card_no LEFT JOIN custdata as c on d.card_no=c.cardno and c.personnum=1 where noticesThisYear <= 1 and noticesThisQuarter = 0 and c.type <> 'TERM' and c.memType not in (3,9) group by d.card_no having sum(case when datediff(mm,getdate(),tdate)=-5 then 1 else 0 end) <> 0 and sum(case when datediff(mm,getdate(),tdate)=-4 then 1 else 0 end) <> 0 and sum(case when datediff(mm,getdate(),tdate)=-3 then 1 else 0 end) <> 0 and sum(case when datediff(mm,getdate(),tdate)=-2 then 1 else 0 end) = 0 and sum(case when datediff(mm,getdate(),tdate)=-1 then 1 else 0 end) = 0""" % dlog print query import Sybase conn = Sybase.connect() db = conn.cursor() db.execute("use WedgePOS") db.execute(query) conn.commit()
def find_images(cdata,boxsize): import os intersect_info = {} ################################################# # get datasets that contain cutout circle center ################################################# db = Sybase.connect('SYBASE', user_name, user_passwd, database=db_name) dbcmd = """ SELECT w.file_id,w.ext_no,creation_date, CASE WHEN release_date<getdate() THEN 1 ELSE 0 END release, w.* FROM wcsInfo w JOIN cfht_received r ON w.dataset_name=r.dataset_name JOIN exposure e ON e.expnum=r.expnum WHERE w.htm_index LIKE '%s%s' ORDER BY creation_date """ % (cdata['htm_index'],'%') c = db.cursor() c.execute(dbcmd) wcsInfo = c.fetchall() import wcsutil intersect_dataset = [] intersect_extnum = [] intersect_cutout=[] intersect_date=[] intersect_public=[] import string largest_area_value=-1 largest_area_dataset='' for dataset in wcsInfo: ### build a WCSObject wcsDict={} for i,elt in enumerate(dataset): wcsDict[string.upper(c.description[i][0])]=elt wcs=wcsutil.WCSObject(wcsDict) (cutout, area)=circTOcutout(wcs,cdata['RA'],cdata['DEC'],max(boxsize,cdata['RADIUS'])) key=str(dataset[0])+string.zfill(int(dataset[1]),2) if cutout==None: continue if area>largest_area_value: largest_area_value = area largest_area_dataset = key intersect_cutout.append( cutout) intersect_dataset.append(str(dataset[0])) intersect_extnum.append( str(dataset[1])) intersect_date.append(str(dataset[2])) intersect_public.append(str(dataset[3])) c.close() db.close() ############################################################## # construct dictionary of nested dictionaries to be returned ############################################################## intersect_info['dataset'] = intersect_dataset intersect_info['extnum'] = intersect_extnum intersect_info['cutout'] = intersect_cutout intersect_info['date'] = intersect_date intersect_info['public'] = intersect_public data_ok = -1 if len(intersect_dataset)>0: data_ok = 1 return largest_area_dataset,intersect_info, data_ok
def __init__(self, p_db, connect = True): ''' Parameter db="dbtype://*****:*****@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%s))(CONNECT_DATA=(SERVICE_NAME=%s)))" s = s % (user, pwd, hostname, port, dbname) self.db = cx_Oracle.connect(s) self.cursor = self.db.cursor() elif self.dbtype == "netezza": # conn="DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; PORT=3306; DATABASE=mysql; UID=joe; # PASSWORD=bloggs; OPTION=3;SOCKET=/var/run/mysqld/mysqld.sock;" self.cursor = Connect(hostname, user, pwd) elif self.dbtype in ["hana"]: from hdbcli import dbapi self.db = dbapi.connect( address=hostname, port=30015+int(port), user=user, password=pwd, autocommit=True) self.cursor = self.db.cursor() elif self.dbtype in ["progress"]: dsn = self.odbc(hostname, port, dbname) self.db = pyodbc.connect( dsn=dsn, user=user, password=pwd, autocommit=True) self.cursor = self.db.cursor() elif self.dbtype in ["zen"]: # Example: driver={Pervasive ODBC Interface};server=localhost;DBQ=demodata' # Example: driver={Pervasive ODBC Interface};server=hostname:port;serverdsn=dbname' dsn = dbname connString = "DRIVER={Pervasive ODBC Interface};SERVER=%s;ServerDSN=%s;UID=%s;PWD=%s;" % ( hostname, dsn, user, pwd) if connect: self.db = pyodbc.connect(connString, autocommit=True) self.cursor = self.db.cursor() elif self.dbtype in ["ingres", "vector", "vectorh", "actianx", "avalanche"]: connString = "DRIVER={Ingres};SERVER=@%s,tcp_ip,%s;DATABASE=%s;SERVERTYPE=INGRES;UID=%s;PWD=%s;" % ( hostname, port, dbname, user, pwd) if connect: self.db = pyodbc.connect(connString, autocommit=True) self.cursor = self.db.cursor() else: perror("Unknown_db_type", self.dbtype) except Exception as ex: self.logger.exception(ex)
def checkDBtables(tmPrefsOnly): okay = False data = pkgutil.get_data(__package__, 'database.dat') values = re.split("\W+", data) try: db = Sybase.connect(values[0], values[1], values[2], values[3]) c = db.cursor() c.execute("select name from tm_prefs") data = c.fetchall() okay = True logger.info("tm_prefs exists") except (SystemExit, KeyboardInterrupt): raise except Exception: logger.info("tm_prefs is missing") okay = False if not okay: sql = "create table tm_prefs (name varchar(64) not null, param varchar(64) not null, value varchar(255) null, primary key (name, param) )" try: c.execute(sql) okay = True except (SystemExit, KeyboardInterrupt): raise except Exception: logger.error('Failed', exc_info=True) if tmPrefsOnly: c.close() db.close() return okay okay2 = False data = pkgutil.get_data(__package__, 'database.dat') values = re.split("\W+", data) try: db = Sybase.connect(values[0], values[1], values[2], values[3]) c = db.cursor() c.execute("SELECT user_id FROM user_privileges") data = c.fetchall() okay2 = True logger.info("user_privileges exists") except (SystemExit, KeyboardInterrupt): raise except Exception: okay2 = False if not okay2: sql = "create table user_privileges (privilege varchar(30) not null, availability int not null, group_id varchar(30) null, user_id varchar(30) null, description varchar(128) null )" try: c.execute(sql) okay2 = True except (SystemExit, KeyboardInterrupt): raise except Exception: logger.error('Failed', exc_info=True) okay3 = False data = pkgutil.get_data(__package__, 'database.dat') values = re.split("\W+", data) try: db = Sybase.connect(values[0], values[1], values[2], values[3]) c = db.cursor() c.execute("SELECT indx FROM user_privileges_lc") data = c.fetchall() okay3 = True logger.info("user_privileges_lc exists") except (SystemExit, KeyboardInterrupt): raise except Exception: okay3 = False if not okay3: sql = "create table user_privileges_lc (indx int not null, privilege varchar(30) not null, description varchar(128) null, licensed int not null )" try: c.execute(sql) okay3 = True except (SystemExit, KeyboardInterrupt): raise except Exception: logger.error('Failed', exc_info=True) okay4 = False data = pkgutil.get_data(__package__, 'database.dat') values = re.split("\W+", data) try: db = Sybase.connect(values[0], values[1], values[2], values[3]) c = db.cursor() c.execute("SELECT * FROM groups") data = c.fetchall() okay4 = True logger.info("groups exists") except (SystemExit, KeyboardInterrupt): raise except Exception: okay4 = False if not okay4: sql = "create table groups (group_name varchar(30) not null, user_id varchar(30) null )" try: c.execute(sql) okay4 = True except (SystemExit, KeyboardInterrupt): raise except Exception: logger.error('Failed', exc_info=True) sql = "insert into groups (group_name, user_id) values ('everyone', '')" try: c.execute(sql) okay4 = True except (SystemExit, KeyboardInterrupt): raise except Exception: logger.error('Failed', exc_info=True) c.close() db.close() return okay and okay2 and okay3 and okay4
action="store", default=1.0, help="How many sigma from the prediction are you willing to look?") (opt,args)=parser.parse_args() if not opt.date or not opt.abg: parser.print_help() parser.error("You must give both an ABG file and a DATE to search for observations on\n") sys.exit(0) import sys, RO.StringUtil if opt.verbose: sys.stderr.write("Opening a connection to SYBASE\n") db = Sybase.connect('SYBASE', user_name, user_passwd, database=db_name) obj=predict(opt.abg,opt.date) import string date=string.replace(opt.date,' ','/') dbcmd=""" SELECT distinct(r.expnum) FROM wcsInfo w JOIN cfht_received r ON w.dataset_name=r.dataset_name WHERE w.htm_index like '%s%s' ORDER BY expnum """ % ( htmIndex(obj['RA'],obj['DEC'],htm_level=5),'%') if opt.verbose: sys.stderr.write("Searching Archive for images at correct location using command\n %s\n" % ( dbcmd,)) c = db.cursor()
print '\nError while reading the MySQL database! \n' print 'Error %d: %s' % (e.args[0], e.args[1]) print 'Will exit with status -99...' sys.exit(-99) #Closes the cursor and the connection cursor.close() DB.close() #returns 2D-array return result if __name__ == '__main__': """ No proper test section written. Do NOT try to execute this file! """ import sys sys.exit('Do NOT try to execute this file!') import string, Sybase db = Sybase.connect('SYBASE', 'sa', '', 'sybsystemprocs') c = db.cursor() if len(sys.argv) > 1: c.execute('select c.text from syscomments c, sysobjects o' ' where o.name = @name and o.type = "P" and c.id = o.id' ' order by c.colid', {'@name': sys.argv[1]}) print string.join([row[0] for row in c.fetchall()], '') else: c.execute('select name from sysobjects where type = "P" order by name') print string.join([row[0] for row in c.fetchall()], '\n')
def find_images(cdata, boxsize): import os intersect_info = {} ################################################# # get datasets that contain cutout circle center ################################################# db = Sybase.connect('SYBASE', user_name, user_passwd, database=db_name) dbcmd = """ SELECT w.file_id,w.ext_no,creation_date, CASE WHEN release_date<getdate() THEN 1 ELSE 0 END release, w.* FROM wcsInfo w JOIN cfht_received r ON w.dataset_name=r.dataset_name JOIN exposure e ON e.expnum=r.expnum WHERE w.htm_index LIKE '%s%s' ORDER BY creation_date """ % (cdata['htm_index'], '%') c = db.cursor() c.execute(dbcmd) wcsInfo = c.fetchall() import wcsutil intersect_dataset = [] intersect_extnum = [] intersect_cutout = [] intersect_date = [] intersect_public = [] import string largest_area_value = -1 largest_area_dataset = '' for dataset in wcsInfo: ### build a WCSObject wcsDict = {} for i, elt in enumerate(dataset): wcsDict[string.upper(c.description[i][0])] = elt wcs = wcsutil.WCSObject(wcsDict) (cutout, area) = circTOcutout(wcs, cdata['RA'], cdata['DEC'], max(boxsize, cdata['RADIUS'])) key = str(dataset[0]) + string.zfill(int(dataset[1]), 2) if cutout == None: continue if area > largest_area_value: largest_area_value = area largest_area_dataset = key intersect_cutout.append(cutout) intersect_dataset.append(str(dataset[0])) intersect_extnum.append(str(dataset[1])) intersect_date.append(str(dataset[2])) intersect_public.append(str(dataset[3])) c.close() db.close() ############################################################## # construct dictionary of nested dictionaries to be returned ############################################################## intersect_info['dataset'] = intersect_dataset intersect_info['extnum'] = intersect_extnum intersect_info['cutout'] = intersect_cutout intersect_info['date'] = intersect_date intersect_info['public'] = intersect_public data_ok = -1 if len(intersect_dataset) > 0: data_ok = 1 return largest_area_dataset, intersect_info, data_ok
#!/usr/bin/python import datetime import Sybase today = datetime.date.today() fp = open('../../src/Credentials/GenericDB.python') line = fp.read().strip() fp.close() hostname,username,pw = line.split(",") conn = Sybase.connect(hostname,username,pw) db = conn.cursor() db.execute('use WedgePOS') dlog = "(" curmonth = today.month curyear = today.year for i in xrange(12): curmonth -= 1 if curmonth == 0: curyear -= 1 curmonth = 12 dlog += "select * from trans_archive.dbo.dlog"+str(curyear)+str(curmonth).zfill(2) if i < 11: dlog += " union all " dlog += ")" query = """INSERT INTO YTD_Patronage_Speedup select d.card_no,datepart(mm,d.tdate) as month_no,
def handle(self, *args, **options): rx_dept_code = re.compile(ur'([a-zA-Z]+)(\d+)') host = options.get('host', None) user = options.get('user', None) password = options.get('password', None) encoding = options.get('encoding', 'cp949') exclude_lecture = options.get('exclude_lecture', False) lecture_count = 0 try: if password is None: password = getpass.getpass() except (KeyboardInterrupt, EOFError): print return try: db = Sybase.connect(host, user, password, 'scholar') except Sybase.DatabaseError: print>>sys.stderr, 'Connection failed. Check username and password.' return c = db.cursor() if not exclude_lecture: c.execute('SELECT * FROM view_OTL_charge WHERE lecture_year = %d AND lecture_term = %d' % (settings.NEXT_YEAR, settings.NEXT_SEMESTER)) professors = c.fetchall() c.execute('SELECT * FROM view_OTL_lecture WHERE lecture_year = %d AND lecture_term = %d ORDER BY dept_id' % (settings.NEXT_YEAR, settings.NEXT_SEMESTER)) rows = c.fetchall() departments = {} lectures_not_updated = set() for lecture in Lecture.objects.filter(year = settings.NEXT_YEAR, semester = settings.NEXT_SEMESTER): lectures_not_updated.add(( lecture.code, lecture.year, lecture.semester, lecture.department.id, lecture.class_no, )) # Make Staff Professor with ID 830 try: staff_professor = Professor.objects.get(professor_id=830) except Professor.DoesNotExist: staff_professor = Professor.objects.create(professor_id=830) staff_professor.professor_name = 'Staff' staff_professor.professor_name_en = 'Staff' staff_professor.save() prev_department = None for row in rows: myrow = [] for elem in row: if isinstance(elem, str): try: elem = elem.decode(encoding) except UnicodeDecodeError: eleme = u'%s (???)' % row[20] print>>sys.stderr, 'ERROR: parsing error on lecture %s' % row[20] print>>sys.stderr, ' cannot read "%s" in cp949.' % elem myrow.append(elem) # Extract department info. lecture_no = myrow[2] lecture_code = myrow[20] lecture_class_no = myrow[3].strip() department_no = lecture_no[0:2] department_id = int(myrow[4]) department_code = rx_dept_code.match(lecture_code).group(1) # Update department info. if prev_department != department_id: try: department = Department.objects.get(id = department_id) print 'Updating department: %s' % department except Department.DoesNotExist: department = Department(id = department_id) print 'Adding department: %s(%d)...' % (department_code, department_id) department.num_id = department_no department.code = department_code department.name = myrow[5] department.name_en = myrow[6] department.save() prev_department = department_id # Extract lecture info. #try: #print 'Retreiving %s: %s [%s]...' % (lecture_code, myrow[7].encode('utf-8'), lecture_class_no) #except UnicodeDecodeError: #print 'Retreiving %s: ??? [%s]...' % (lecture_code, lecture_class_no) #myrow[7] = u'???' lecture_key = { 'code': lecture_no, 'year': int(myrow[0]), 'semester': int(myrow[1]), 'department': Department.objects.get(id = department_id), 'class_no': lecture_class_no, } # Convert the key to a hashable object (tuple). lecture_key_hashable = ( lecture_no, int(myrow[0]), int(myrow[1]), department_id, lecture_class_no, ) try: lecture = Lecture.objects.get(**lecture_key) print 'Updating existing lecture...' except Lecture.DoesNotExist: lecture = Lecture(**lecture_key) lecture.num_people = 0 print 'Creating new lecture...' # Update lecture info. lecture.old_code = myrow[20] lecture.title = myrow[7] lecture.title_en = myrow[8] lecture.type = myrow[10] # 과목구분 (한글) lecture.type_en = myrow[11] # 과목구분 (영문) lecture.audience = int(myrow[12]) # 학년 구분 lecture.limit= myrow[17] # 인원제한 lecture.credit = myrow[16] # 학점 lecture.credit_au = myrow[13] # AU lecture.num_classes = int(myrow[14]) # 강의시수 lecture.num_labs = int(myrow[15]) # 실험시수 lecture.notice = myrow[19] # 비고 lecture.is_english = True if myrow[21] == 'Y' else False # 영어강의 여부 lecture.deleted = False # Course save try: course = Course.objects.get(old_code=lecture.old_code) course.department = department course.type = lecture.type course.type_en = lecture.type_en course.title = lecture.title.split("<")[0].split("[")[0] course.title_en = lecture.title_en.split("<")[0].split("[")[0] course.save() # print "Updating Course ... %s" % course.title except Course.DoesNotExist: course = Course() course.old_code = lecture.old_code course.department = department course.type = lecture.type course.type_en = lecture.type_en course.title = lecture.title.split("<")[0].split("[")[0] course.title_en = lecture.title_en.split("<")[0].split("[")[0] course.score_average = 0 course.load_average = 0 course.gain_average = 0 course.save() # print "Making new Course ... %s" % course.title lecture.course = course lecture.save() lecture_count += 1 # professor save match_scholar = filter(lambda a: lecture.year == a[0] and lecture.semester == a[1] and lecture.code == a[2] and lecture.class_no.strip() == a[3].strip() and lecture.department_id == a[4], professors) if len(match_scholar) != 0: professors_not_updated = set() for prof in lecture.professor.all(): professors_not_updated.add(prof.id) for i in match_scholar: try: prof_id = i[5] prof_name = unicode(i[6], 'cp949') if i[8] is None or i[8]=='': prof_name_en = '' else: prof_name_en = unicode(i[8].strip(),'cp949') professor = Professor.objects.get(professor_id=prof_id) if professor.professor_name != prof_name and prof_id !=830: professor.professor_name = prof_name professor.save() if professor.professor_name_en != prof_name_en and prof_id != 830 and prof_name_en!='': professor.professor_name_en = prof_name_en professor.save() professors_not_updated.remove(professor.id) except Professor.DoesNotExist: professor = Professor.objects.create(professor_id=prof_id) professor.professor_name = prof_name professor.professor_name_en = prof_name_en professor.save() # print "Making new Professor ... %s" % professor.professor_name except KeyError: pass lecture.professor.add(professor) if professor.professor_id != 830: lecture.course.professors.add(professor) else: lecture.professor.add(staff_professor) for key in professors_not_updated: professor = Professor.objects.get(id=key) lecture.professor.remove(professor) try: lectures_not_updated.remove(lecture_key_hashable) except KeyError: pass c.close() # Extract exam-time, class-time info. print 'Extracting exam time information...' c = db.cursor() c.execute('SELECT * FROM view_OTL_exam_time WHERE lecture_year = %d AND lecture_term = %d' % (settings.NEXT_YEAR, settings.NEXT_SEMESTER)) exam_times = c.fetchall() c.close() ExamTime.objects.filter(lecture__year__exact=settings.NEXT_YEAR, lecture__semester=settings.NEXT_SEMESTER).delete() for row in exam_times: myrow = [] for elem in row: if isinstance(elem, str): elem = elem.decode(encoding) myrow.append(elem) lecture_key = { 'code': myrow[2], 'year': int(myrow[0]), 'semester': int(myrow[1]), 'department': Department.objects.filter(id = int(myrow[4]))[0], 'class_no': myrow[3].strip(), } try: lecture = Lecture.objects.get(**lecture_key) exam_time = ExamTime(lecture=lecture) exam_time.day = int(myrow[5]) - 1 exam_time.begin = time(hour=myrow[6].hour, minute=myrow[6].minute) exam_time.end = time(hour=myrow[7].hour, minute=myrow[7].minute) print 'Updating exam time for %s' % lecture exam_time.save() except Lecture.DoesNotExist: print 'Exam-time for non-existing lecture %s; skip it...' % myrow[2] print 'Extracting class time information...' c = db.cursor() c.execute('SELECT * FROM view_OTL_time WHERE lecture_year = %d AND lecture_term = %d' % (settings.NEXT_YEAR, settings.NEXT_SEMESTER)) class_times = c.fetchall() c.close() ClassTime.objects.filter(lecture__year__exact=settings.NEXT_YEAR, lecture__semester=settings.NEXT_SEMESTER).delete() for row in class_times: myrow = [] for elem in row: if isinstance(elem, str): elem = elem.decode(encoding) myrow.append(elem) lecture_key = { 'code': myrow[2], 'year': int(myrow[0]), 'semester': int(myrow[1]), 'department': Department.objects.filter(id = int(myrow[4]))[0], 'class_no': myrow[3].strip(), } try: lecture = Lecture.objects.get(**lecture_key) class_time = ClassTime(lecture=lecture) class_time.day = int(myrow[5]) - 1 class_time.begin = time(hour=myrow[6].hour, minute=myrow[6].minute) class_time.end = time(hour=myrow[7].hour, minute=myrow[7].minute) class_time.type = myrow[8] class_time.building = myrow[9] class_time.room = myrow[10] class_time.room_ko = myrow[12] class_time.room_en = myrow[13] try: class_time.unit_time = int(myrow[11]) except (ValueError, TypeError): class_time.unit_time = 0 print 'Updating class time for %s' % lecture class_time.save() except Lecture.DoesNotExist: print 'Class-time for non-existing lecture %s; skip it...' % myrow[2] ''' c = db.cursor() c.execute('SELECT * FROM view_OTL_syllabus WHERE lecture_year = %d AND lecture_term = %d' % (settings.NEXT_YEAR, settings.NEXT_SEMESTER)) syllabuses = c.fetchall() c.close() Syllabus.objects.filter(lecture__year__exact=settings.NEXT_YEAR, lecture__semester=settings.NEXT_SEMESTER).delete() for row in syllabuses: myrow = [] for elem in row: if isinstance(elem, str): try: elem = elem.decode(encoding) except UnicodeDecodeError: eleme = u'%s (???)' % row[2] print>>sys.stderr, 'ERROR: parsing error on lecture %s' % row[2] print>>sys.stderr, ' cannot read "%s" in cp949.' % elem myrow.append(elem) lecture_key = { 'code': myrow[2], 'year': int(myrow[0]), 'semester': int(myrow[1]), 'department': Department.objects.filter(id = int(myrow[4]))[0], 'class_no': myrow[3].strip(), } try: lecture = Lecture.objects.get(**lecture_key) syllabus = Syllabus(lecture=lecture) syllabus.professor_info = myrow[5] syllabus.abstract = myrow[6] syllabus.evluation = myrow[7] syllabus.materials = myrow[8] syllabus.plan = myrow[9] syllabus.etc = myrow[10] syllabus.url = myrow[11] syllabus.attachment = myrow[12] print 'Updating syllabus information for %s' % lecture syllabus.save() except Lecture.DoesNotExist: print 'Syllabus information for non-existing lecture %s; skip it...' % myrow[2] ''' if not exclude_lecture: # Mark deleted lectures to notify users. print 'Marking deleted lectures...' for key in lectures_not_updated: lecture_key = { 'code': key[0], 'year': key[1], 'semester': key[2], 'department': Department.objects.get(id = key[3]), 'class_no': key[4], } lecture = Lecture.objects.get(**lecture_key) lecture.deleted = True # print '%s is marked as deleted...' % lecture lecture.save() db.close() print '\nTotal number of departments : %d' % Department.objects.count() print 'Total number of lectures newly added : %d' % lecture_count
#!/usr/bin/python import datetime import Sybase today = datetime.date.today() fp = open('../../src/Credentials/GenericDB.python') line = fp.read().strip() fp.close() hostname, username, pw = line.split(",") conn = Sybase.connect(hostname, username, pw) db = conn.cursor() db.execute('use WedgePOS') dlog = "(" curmonth = today.month curyear = today.year for i in xrange(12): curmonth -= 1 if curmonth == 0: curyear -= 1 curmonth = 12 dlog += "select * from trans_archive.dbo.dlog" + str(curyear) + str( curmonth).zfill(2) if i < 11: dlog += " union all " dlog += ")" query = """INSERT INTO YTD_Patronage_Speedup
def updatePrivBasedOnLicensing(): data = pkgutil.get_data(__package__, 'database.dat') values = re.split("\W+", data) try: db = Sybase.connect(values[0], values[1], values[2], values[3]) c = db.cursor() sql = "select distinct user_privileges_lc.privilege, user_privileges_lc.description from user_privileges, user_privileges_lc where availability = 0 and user_privileges.privilege = user_privileges_lc.privilege and licensed = 0 " c.execute(sql) data = c.fetchall() for row in data: privilege = row[0] description = row[1] sql = "delete from user_privileges where privilege = '" + privilege + "' and availability = 0" c.execute(sql) sql = "insert into user_privileges (privilege, availability, group_id, user_id, description) values ('" + privilege + "', 1, '', '', '" + description + "')" c.execute(sql) bEveryoneCheck = False sql = "select distinct user_privileges_lc.privilege, user_privileges_lc.description from user_privileges, user_privileges_lc where availability = 1 and user_privileges.privilege = user_privileges_lc.privilege and licensed = 1 " " and user_privileges_lc.privilege in ('omusl_manage_patients', 'omusl_manage_studies', 'omusl_push_monitor', 'omusl_run', 'omusl_study_status'," " 'omv_add_report', 'omv_edit_report', 'omv_email', 'omv_push', 'omv_save_anno', 'omv_search', 'omv_show_anno', 'omv_view', 'omx_multy', 'omx_run', 'omusl_vcd'," " 'allpro_images', 'omusl_wklst_scu', 'omusl_scanner', 'omusl_attach', 'omusl_non_dicom', 'omusl_lightscribe', 'omusl_cd_import', 'omusl_jpeg_export'," " 'omv_adv_anno', 'omv_https', 'omusl_radviewer')" c.execute(sql) data = c.fetchall() for row in data: privilege = row[0] description = row[1] sql = "delete from user_privileges where privilege = '" + privilege + "' and availability = 1" c.execute(sql) sql = "insert into user_privileges (privilege, availability, group_id, user_id, description) values ('" + privilege + "', 0, 'everyone', '', '" + description + "')" c.execute(sql) bEveryoneCheck = True if (bEveryoneCheck): sql = "SELECT group_name FROM groups WHERE group_name = 'everyone'" c.execute(sql) data = c.fetchall() if (data[0][0] != ""): sql = "insert into groups (group_name) values ('everyone', '')" c.execute(sql) bAdminCheck = False sql = "select distinct user_privileges_lc.privilege, user_privileges_lc.description from user_privileges, user_privileges_lc where availability = 1 and user_privileges.privilege = user_privileges_lc.privilege and licensed = 1 " " and user_privileges_lc.privilege in ('omacm_add_priv', 'omacm_admin', 'omadmin_cc', 'omadmin_console', 'omadmin_db_check', 'omadmin_dict', 'omadmin_distr'," " 'omadmin_erpr', 'omadmin_file_audit', 'omadmin_flex', 'omadmin_hp', 'omadmin_kds', 'omadmin_push', 'omadmin_run', 'omadmin_utils', 'omsdm_power_on'," " 'omstm_run', 'omstm_admin', 'omusl_profile', 'omv_vitrea', 'pacs_hl7_adv', 'omv_push_adv', 'pacs_ipad', 'pacs_android', 'omx_publishing'," " 'omusl_vcd_import', 'omusl_oncall_caching', 'rsvw_dictation', 'omv_print', 'omv_dicom_print', 'omv_multi_monitor', 'autoupdate_run', 'omusl_adv_demo'," " 'omusl_adv_filters', 'pacs_wklst_scp', 'pacs_report_activity', 'pacs_backup', 'pacs_backup_adv', 'pacs_hl7', 'pacs_hl7_adv')" for row in data: privilege = row[0] description = row[1] sql = "delete from user_privileges where privilege = '" + privilege + "' and availability = 1" c.execute(sql) sql = "insert into user_privileges (privilege, availability, group_id, user_id, description) values ('" + privilege + "', 0, '', 'admin', '" + description + "')" c.execute(sql) bAdminCheck = True if (bAdminCheck): sql = "select user_id from users where user_id = 'admin'" c.execute(sql) data = c.fetchall() if (data[0][0] != ""): sql = "insert into users (user_id, name, last_name, first_name, password) values ('admin', 'PACSimple Admin', 'Admin', 'PACSimple', 'admin!')" c.execute(sql) sql = "select privilege from user_privileges where user_id = 'admin' and privilege = 'omadmin_run'" c.execute(sql) data = c.fetchall() if (data[0][0] != ""): c.execute( "select description from user_privileges where privilege = 'omadmin_run'" ) sql = "delete from user_privileges where privilege = 'omadmin_run' and availability = 1" c.execute(sql) data = c.fetchall() sDescription = data[0][0] sql = "insert into user_privileges (privilege, availability, group_id, user_id, description) values ('omadmin_run', 0, '', 'admin', '" + sDescription + "')" c.execute(sql) sql = "select privilege from user_privileges where user_id = 'admin' and privilege = 'omacm_admin'" c.execute(sql) data = c.fetchall() if (data[0][0] != ""): c.execute( "select description from user_privileges where privilege = 'omacm_admin'" ) sql = "delete from user_privileges where privilege = 'omacm_admin' and availability = 1" c.execute(sql) data = c.fetchall() sDescription = data[0][0] sql = "insert into user_privileges (privilege, availability, group_id, user_id, description) values ('omacm_admin', 0, '', 'admin', '" + sDescription + "')" c.execute(sql) sql = "select privilege from user_privileges where user_id = 'admin' and privilege = 'omacm_add_priv'" c.execute(sql) data = c.fetchall() if (data[0][0] != ""): c.execute( "select description from user_privileges where privilege = 'omacm_add_priv'" ) sql = "delete from user_privileges where privilege = 'omacm_add_priv' and availability = 1" c.execute(sql) data = c.fetchall() sDescription = data[0][0] sql = "insert into user_privileges (privilege, availability, group_id, user_id, description) values ('omacm_add_priv', 0, '', 'admin', '" + sDescription + "')" c.execute(sql) c.close() db.close() except (SystemExit, KeyboardInterrupt): raise except Exception: logger.error('updatePrivBasedOnLicensing Failed', exc_info=False)
print 'Error %d: %s' % (e.args[0], e.args[1]) print 'Will exit with status -99...' sys.exit(-99) #Closes the cursor and the connection cursor.close() DB.close() #returns 2D-array return result if __name__ == '__main__': """ No proper test section written. Do NOT try to execute this file! """ import sys sys.exit('Do NOT try to execute this file!') import string, Sybase db = Sybase.connect('SYBASE', 'sa', '', 'sybsystemprocs') c = db.cursor() if len(sys.argv) > 1: c.execute( 'select c.text from syscomments c, sysobjects o' ' where o.name = @name and o.type = "P" and c.id = o.id' ' order by c.colid', {'@name': sys.argv[1]}) print string.join([row[0] for row in c.fetchall()], '') else: c.execute('select name from sysobjects where type = "P" order by name') print string.join([row[0] for row in c.fetchall()], '\n')
# # #--------------------------------------------------------------------------------------------------------# #--------------------------------------------------------------------------------------------------------# #----------------------------------------------Do the thing----------------------------------------------# #--------------------------------------------------------------------------------------------------------# #--------------------------------------------------------------------------------------------------------# all_prop_list = raw_input("All Proposals List?: ") all_prop_list = numpy.loadtxt(all_prop_list, skiprows=0) username = getpass.getuser() userpass = getpass.getpass("DB password: "******"use proposal") # use the proposal dB #---------------------------------------------------# #---------------- Build Main Pages -----------------# #---------------------------------------------------# #make proposal_lists_dictionary proposal_lists_dictionary = assign_subject_categories(all_prop_list) make_directories(all_prop_list) for prop_list in proposal_lists_dictionary: open_file(prop_list)
def makeConnection(self): return Sybase.connect(self.host, self.user, self.password, database=self.db, auto_commit=self.autoCommit, locking=self.locking)
def db_conn(username, userpass): global cur conn = sybase.connect('sqlsao', username, userpass) #establish a connection to the dB cur = conn.cursor() cur.execute("use proposal") # use the proposal dB
def find_images(cdata): import os intersect_info = {} ################################################# # get datasets that contain cutout circle center ################################################# db = Sybase.connect('SYBASE', user_name, user_passwd, database=db_name) dbcmd = """ SELECT w.file_id,w.ext_no,w.* FROM wcsInfo w JOIN cfht_received r ON w.dataset_name=r.dataset_name JOIN exposure e ON e.expnum=r.expnum WHERE w.dataset_name LIKE '%s' AND w.htm_index LIKE '%s%s' AND abs(night-%s) < 1 ORDER BY w.file_id """ % (cdata['dataset_name'],cdata['htm_index'],'%',cdata['night']) c = db.cursor() c.execute(dbcmd) wcsInfo = c.fetchall() import wcsutil intersect_dataset = {} intersect_extnum = {} intersect_cutout={} import string largest_area_value=-1 largest_area_dataset='' for dataset in wcsInfo: ### build a WCSObject wcsDict={} for i,elt in enumerate(dataset): wcsDict[string.upper(c.description[i][0])]=elt wcs=wcsutil.WCSObject(wcsDict) (cutout, area)=circTOcutout(wcs,cdata['RA'],cdata['DEC'],max(0.015,cdata['RADIUS'])) key=str(dataset[0])+string.zfill(int(dataset[1]),2) if cutout==None: continue if area>largest_area_value: largest_area_value = area largest_area_dataset = key intersect_cutout[key]= cutout intersect_dataset[key] = str(dataset[0]) intersect_extnum[key] = str(dataset[1]) c.close() db.close() ############################################################## # construct dictionary of nested dictionaries to be returned ############################################################## intersect_info['dataset'] = intersect_dataset intersect_info['extnum'] = intersect_extnum intersect_info['cutout'] = intersect_cutout data_ok = -1 if len(intersect_dataset)>0: data_ok = 1 return largest_area_dataset,intersect_info, data_ok
def __init__(self, req, provider=None): self.req = req if self.req != None: try: from mod_python import util self.fieldStorage = util.FieldStorage(self.req) except ImportError: self.fieldStorage = {} else: self.fieldStorage = {} self.con = None self.requireSSL = False self.abuseEvent = 1 self.configAbuseEvent = None self.abuseGracePeriod = 1 self.configAbuseGracePeriod = None self.abuseHandler = 1 self.configAbuseHandler = None self.accountType = 1 self.configAccountType = None self.blacklistEntry = 1 self.configBlacklistEntry = None self.dnsRecord = 1 self.configDNSRecord = None self.domain = 1 self.configDomain = None self.dslAccount = 1 self.configDSLAccount = None self.dslBlock = 1 self.configDSLBlock = None self.dslChannel = 1 self.configDSLChannel = None self.equipmentClass = 1 self.configEquipmentClass = None self.equipmentType = 1 self.configEquipmentType = None self.greylistTriplet = 1 self.configGreylistTriplet = None self.internalUser = 1 self.externalSystem = 1 self.configExternalSystem = None self.group = 1 self.configGroup = None self.configInternalUser = None self.mailEvent = 1 self.configMailEvent = None self.mailSenderCacheEntry = 1 self.configMailSenderCacheEntry = None self.manufacturer = 1 self.configManufacturer = None self.masterAccount = 1 self.configMasterAccount = None self.msAddressBookIndividual = 1 self.configMSAddressBookIndividual = None self.msAddressBookGroupEntry = 1 self.configMSAddressBookGroupEntry = None self.msNote = 1 self.configMSNote = None self.networkDevice = 1 self.configNetworkDevice = None self.problem = 1 self.configProblem = None self.problemCategory = 1 self.configProblemCategory = None self.project = 1 self.configProject = None self.projectComment = 1 self.configProjectComment = None self.projectStatus = 1 self.configProjectStatus = None self.projectUser = 1 self.configProjectUser = None self.provider = 1 self.configProvider = None self.region = 1 self.configRegion = None self.subAccount = 1 self.configSubAccount = None self.subWeb = 1 self.configSubWeb = None self.subWebUser = 1 self.configSubWebUser = None self.task = 1 self.configTask = None self.taskDate = 1 self.configTaskDate = None self.taskException = 1 self.configTaskException = None self.taskUser = 1 self.configTaskUser = None self.terminal = 1 self.configTerminal = None self.terminalPhone = 1 self.configTerminalPhone = None self.troubleTicket = 1 self.configTroubleTicket = None self.troubleTicketComment = 1 self.configTroubleTicketComment = None self.wantAdCategory = 1 self.configWantAdCategory = None self.wantAdEntry = 1 self.configWantAdEntry = None self.wirelessAccount = 1 self.configWirelessAccount = None self.web = 1 self.configWeb = None self.webRedirect = 1 self.configWebRedirect = None self.webUser = 1 self.configWebUser = None self.whitelistEntry = 1 self.configWhitelistEntry = None if self.req == None: if provider == None and self.req == None: self.providerString = None return else: self.providerString = str(provider) else: # FIXME: Get Provider from req somehow (probably from the fieldStorage object) self.providerString = 'provider1.example.com' if self.providerString == "provider1.example.com": # TEMP: Connect to the database. try: import dbi import odbc self.con = odbc.odbc('DSN/user/pass') except: try: import Sybase self.con = Sybase.connect('127.0.0.1:1433', 'username', 'password') except: self.con = None self.configMasterAccount = self.provider1ConfigMasterAccount self.configSubAccount = self.provider1ConfigSubAccount self.configWeb = self.provider1ConfigWeb # TEMP: These tables doesn't exist in the database yet. self.dslAccount = 0 self.wirelessAccount = 0 elif self.providerString == "provider2.example.com": self.abuseEvent = 0 self.abuseGracePeriod = 0 self.abuseHandler = 0 self.blacklistEntry = 0 self.dnsRecord = 0 self.dslAccount = 0 self.dslBlock = 0 self.dslChannel = 0 self.equipmentClass = 0 self.equipmentType = 0 self.greylistTriplet = 0 self.internalUser = 0 self.mailEvent = 0 self.mailSenderCacheEntry = 0 self.manufacturer = 0 self.configMasterAccount = self.provider2ConfigMasterAccount self.msAddressBookIndividual = 0 self.msAddressBookGroupEntry = 0 self.msNote = 0 self.networkDevice = 0 self.problem = 0 self.problemCategory = 0 self.project = 0 self.projectComment = 0 self.projectStatus = 0 self.projectUser = 0 self.configSubAccount = self.provider2ConfigSubAccount self.subWeb = 0 self.subWebUser = 0 self.task = 0 self.taskDate = 0 self.taskException = 0 self.taskUser = 0 self.terminal = 0 self.terminalPhone = 0 self.troubleTicket = 0 self.troubleTicketComment = 0 self.wantAdCategory = 0 self.wantAdEntry = 0 self.wirelessAccount = 0 self.web = 0 self.webRedirect = 0 self.webUser = 0 self.whitelistEntry = 0 assert ((self.web and self.webUser) or ((not self.web) and (not self.webUser))), 'webs and webUsers must stick together.' assert (not self.subWeb or (self.subWeb and self.web)), 'One cannot have subWebs without webs to subdivide.' assert (not self.webRedirect or (self.webRedirect and self.web)), 'One cannot have webRedirects without webs to redirect to.' assert (not self.troubleTicketComment or (self.troubleTicketComment and self.troubleTicket)), 'One cannot have troubleTicketComments without troubleTickets to comment on.'