def sql_exe_pg(conf_path, SQL): addr = fileop.parse_conf_peloton(conf_path) try: # Connect to database conn = psycopg2.connect(database=addr['database'], user=addr['user'], password=addr['password'], host=addr['host'], port=addr['port']) print "Connect testdb successfully" # Get the db handler cur = conn.cursor() # Execute SQL statement cur.execute(SQL) # Get the data rows = cur.fetchall() res = Counter() # write the data into multiset for row in rows: res[row] = res[row] + 1 print (row) except psycopg2.DatabaseError, e: print "Unable to execute postgres:%s"%e sys.exit(1)
def sql_exe_pt_tofile(conf_path, SQL, filename): path = '../output/' file_path = path + filename addr = fileop.parse_conf_peloton(conf_path) try: # Connect to database conn = psycopg2.connect(database=addr['database'], user=addr['user'], password=addr['password'], host=addr['host'], port=addr['port']) print "Connect testdb successfully" # Get the db handler cur = conn.cursor() # Insert data insert_data(conn, 'company', 100) insert_data(conn, 'corp', 100) # Execute SQL statement cur.execute(SQL) # Insert data insert_data(conn, 'company', 100) insert_data(conn, 'corp', 100) # Get the data rows = cur.fetchall() # create folder if not exists fileop.create_output_dir(path) # open a file f = open(file_path,'w') # write the data for row in rows: row_line = '%d %s %d %s'%row f.write(row_line) f.write('\n') print (row_line) except psycopg2.DatabaseError, e: print "Unable to execute peloton:%s"%e sys.exit(1)