Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Key Value Pair output
#7
OK here is my full code

Before worrying about the error message. 1st question would be if i have the correct format for line 23

Then next question would be if I have it in the right place.

I am using the Official Python Connector for MySQL.

Thanks in advance.

import os
import sys
import logging
sys.path.insert(0, '/opt')
import json
import mysql.connector


logger = logging.getLogger()
logger.setLevel(logging.INFO)


logger.info("SUCCESS: Connection to DB instance succeeded")

def lambda_handler(event, context):
    cnx = mysql.connector.connect(user=os.environ['user'], database=os.environ['database'], host=os.environ['host'], password=os.environ['password'])
    cursor = cnx.cursor()
        
    query = ("SELECT distinctrow UPC, ItemFriendlyNames, ItemName, Brand, Model, Retailer, ItemID FROM my.view order by UPC, Model, Retailer")
        
    cursor.execute(query)
    rows = cursor.fetchall()
    json_rows = [dict(zip(('UPC', 'ItemFriendlyNames', 'ItemName', 'Brand', 'Model', 'Retailer', 'ItemID'), (str(key), value, value, value, value, value, value))) for key, value in rows]
    
    cursor.close()
    cnx.close()

    return {
        'statusCode': 200,
        'headers': {'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'},
         "body": json.dumps(rows)
    }
    
    
    
    
Reply


Messages In This Thread
Key Value Pair output - by UtiliseIT - May-02-2019, 05:57 AM
RE: Key Value Pair output - by buran - May-02-2019, 06:06 AM
RE: Key Value Pair output - by UtiliseIT - May-02-2019, 06:45 AM
RE: Key Value Pair output - by buran - May-02-2019, 06:59 AM
RE: Key Value Pair output - by UtiliseIT - May-02-2019, 07:00 AM
RE: Key Value Pair output - by buran - May-02-2019, 07:02 AM
RE: Key Value Pair output - by UtiliseIT - May-03-2019, 04:41 AM
RE: Key Value Pair output - by buran - May-03-2019, 07:06 AM
RE: Key Value Pair output - by UtiliseIT - May-03-2019, 07:20 AM
RE: Key Value Pair output - by buran - May-03-2019, 07:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting the maximum value:key pair from a dictionary sean1 2 1,547 Jan-17-2022, 01:04 PM
Last Post: DeaD_EyE
  How to extract specific key value pair from string? aditi06 0 2,673 Apr-15-2021, 06:26 PM
Last Post: aditi06
  Auto re-pair / re-sync Controller via Script? User3000 2 2,497 Nov-30-2020, 11:42 AM
Last Post: User3000
  team pair issue jk91 29 8,927 Mar-03-2020, 06:15 PM
Last Post: jefsummers
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 7,148 May-29-2019, 02:27 AM
Last Post: heiner55
  Parsing Text file having repeated value key pair using python manussnair 3 3,433 Aug-04-2018, 11:48 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020