Example #1
0
def store_into_db(results, table_items):
    cur_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print('cur_time: {}'.format(cur_time))
    conn, cur = db_util.get_cur()
    update_cnt = 0
    insert_cnt = 0
    for result in results:
        new_result = [ele if ele != '' else None for ele in result]
        result = new_result
        stock_code = result[0]
        sql = "SELECT `{}` FROM {} WHERE `stock_code` = %s".format('`, `'.join(table_items), table_name)
        db_data = db_util.execute_query(conn, cur, sql, [stock_code])
        if len(db_data) == 0:  # if not found in db, insert
            sql = "INSERT INTO {}(`{}`, `insert_time`, `update_time`) VALUES(".format(
                table_name, '`, `'.join(table_items)) + "%s, " * (len(table_items) + 1) + "%s)"
            db_util.execute_query(conn, cur, sql, result + [cur_time, cur_time])
            insert_cnt += 1
        else:   # check which item need to update
            for item_idx in range(len(result)):
                field = table_items[item_idx]
                old_value = db_data[0][item_idx]
                new_value = result[item_idx]
                if str(new_value) == str(old_value):  # no change
                    continue
                if type(old_value) is float and new_value is not None and float(new_value) == old_value:
                    continue
                update_item(conn, cur, stock_code, field, old_value, new_value, cur_time)
                update_cnt += 1
    print('insert row num: {}\t update item num: {}'.format(insert_cnt, update_cnt))
Example #2
0
def saveformfields(formid, fieldid, value):
    conn = make_conn()
    #query_cmd = "UPDATE FieldDetails SET FieldValue = 'PANKAJ' WHERE FieldMasterID = 1 AND TemplateCustomerMappingID = 14"
    query_cmd = "UPDATE FieldDetails SET FieldValue = '" + str(value) + "' WHERE " \
                "FieldMasterID = " + str(fieldid) + " AND TemplateCustomerMappingID = " + str(formid)
    execute_query(conn, query_cmd)
    conn.close()
    return "OK"
Example #3
0
def update_item(conn, cur, stock_code, field, old_value, new_value, cur_time):
    # update item
    sql = "UPDATE {} SET `{}` = %s, update_time = %s WHERE `stock_code` = %s" .format(table_name, field)
    db_util.execute_query(conn, cur, sql, [new_value, cur_time, stock_code])
    # get update_log
    sql = "SELECT `update_log` FROM {} WHERE `stock_code` = %s".format(table_name)
    temp = db_util.execute_query(conn, cur, sql, [stock_code])
    json_log = [] if temp[0][0] is None or temp[0][0] == '' else json.loads(temp[0][0])
    json_log.insert(0, {'old_value': old_value, 'new_value': new_value,
                        'field': field, 'update_time': cur_time})
    # add record into update_log
    sql = "UPDATE {} SET `update_log` = %s WHERE `stock_code` = %s" .format(table_name)
    db_util.execute_query(conn, cur, sql, [json.dumps(json_log, ensure_ascii=False), stock_code])
    print('stock_code: {}, field: {}, old_value: {}, new_value: {}'.format(
        stock_code, field, old_value, new_value))
Example #4
0
def get_stock_code(exchange=None):
    conn, cur = db_util.get_cur()
    sql = "SELECT `stock_code`, `stock_name`, `board` FROM {} WHERE length(stock_code) = {}".\
        format(table_name, 6 if exchange is None else 5)
    results = db_util.execute_query(conn, cur, sql)
    cur.close()
    conn.close()
    return results
Example #5
0
def getformNumber(formnumber):
    conn = make_conn()
    TemplateCustomerMappingID = ''

    query_cmd = "INSERT INTO TemplateCustomerMapping(UniqueFormNumber, TemplateMasterID) " \
                "VALUES(0, " + str(formnumber) + ") RETURNING TemplateCustomerMappingID"
    # query_cmd = "INSERT INTO TemplateCustomerMapping (UniqueFormNumber, TemplateMasterID) VALUES(0, 1) RETURNING TemplateCustomerMappingID "
    TemplateCustomerMappingID = execute_query_value(conn, query_cmd)

    query_cmd = "INSERT INTO FieldDetails(FieldValue, FieldMasterID, TemplateCustomerMappingID) " \
                "SELECT '', FieldMasterID, " + str(TemplateCustomerMappingID) + \
                " from FieldMaster WHERE TemplateMasterID = " + str(formnumber) + ";"
    # query_cmd = "INSERT INTO FieldDetails(FieldValue, FieldMasterID, TemplateCustomerMappingID) SELECT '', FieldMasterID, " + str(TemplateCustomerMappingID) + " from FieldMaster WHERE TemplateMasterID = 1;"
    result = execute_query(conn, query_cmd)
    conn.close()
    return TemplateCustomerMappingID
Example #6
0
def updateOperationCompleted():
    conn = make_conn()
    query_cmd = "update currentoperation set operationid = 2;"
    execute_query(conn, query_cmd)
    conn.close()
    return "OK"
Example #7
0
def updateOperationFormDetails():
    conn = make_conn()
    query_cmd = "update currentoperation set operationid = 1;"
    execute_query(conn, query_cmd)
    conn.close()
    return "OK"
Example #8
0
def updateOperationWelcome():
    conn = make_conn()
    query_cmd = "update currentoperation set operationid = 0;"
    execute_query(conn, query_cmd)
    conn.close()
    return "OK"