def lambda_handler()

in lambda/hello_world/app.py [0:0]


def lambda_handler(event, context):
    global existing_connection
    if not existing_connection:
        existing_connection = build_conn()
    else:
        print('[INFO] reusing the existing connection')

    try:
        with existing_connection.cursor() as cursor:
            # Create a new record
            sql = "INSERT INTO `test_tables` (`name`, `age`) VALUES (%s, %s)"
            cursor.execute(sql, ('pahud', '20'))

        # connection is not autocommit by default. So you must commit to save
        # your changes.
        existing_connection.commit()

        with existing_connection.cursor() as cursor:
            # Read a single record
            sql = "SELECT count(*) as cnt FROM `test_tables`"
            cursor.execute(sql)
            result = cursor.fetchone()
            print(result)
    finally:
        print('[INFO] leave the connection open')
        # existing_connection.close()

    return respond(None, result)