Python Forum
Looking for an up to date example to query mysql
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looking for an up to date example to query mysql
#2
I am not sure I fully understand your question, but json.dumps(rows) will create string and your function will return a dict. I think what you actually need is to make the return like this
return json.dumps({
    'statusCode':200,
    'headers': {'Access-Control-Allow-Origin': '*'},
    'body': rows
    })
full example
import json

def handler():
    # assume here it retrieve rows from db
    rows = [[""], ["Adelaide"], ["Alexandria"], ["Alice Springs"]]
    
    return json.dumps({
    'statusCode':200,
    'headers': {'Access-Control-Allow-Origin': '*'},
    'body': rows
    })
    
print(handler())
Output:
{"statusCode": 200, "headers": {"Access-Control-Allow-Origin": "*"}, "body": [[""], ["Adelaide"], ["Alexandria"], ["Alice Springs"]]} >>>
as you can see it's a valid json
i.e. I think your function should return str that is valid json, not dict
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Looking for an up to date example to query mysql - by buran - Feb-18-2019, 11:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 346 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 764 Jan-20-2024, 04:45 AM
Last Post: 1418
  Mysql and mysql.connector error lostintime 2 779 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,541 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  Date format and past date check function Turtle 5 4,473 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Problem Using SQL Placeholder In MySQL Query AdeS 11 6,358 Jul-31-2021, 12:19 AM
Last Post: Pedroski55
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,297 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  How to add date and years(integer) to get a date NG0824 4 2,967 Sep-03-2020, 02:25 PM
Last Post: NG0824
  MYSQL Update Query format simdo01 1 2,287 Aug-31-2020, 12:59 AM
Last Post: nilamo
  Python mysql query help please tduckman 4 4,408 Mar-13-2020, 03:42 PM
Last Post: Marbelous

Forum Jump:

User Panel Messages

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