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
#1
Hi,

My use case is to query a MySQL database as part of a (AWS) Lambda function that returns results in json format via an api. Queries may be selects, selects with variables, or firing sp's.

My current situation is I have a working example that I think is quite old. In addition im note sure that the output is in the correct json format. An example from the api is

Output:
[[""], ["Adelaide"], ["Alexandria"], ["Alice Springs"]]
My current code is
def handler(event, context):
    try:
        conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=20)
    except:
        logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
    """
    This function fetches content from mysql RDS instance
    """
    rows = []
    with conn.cursor() as cursor:

        sql = "SELECT * FROM tablea"
        cursor.execute(sql)
        for row in cursor:
            rows.append(row)
            print(row)

    return {
    'statusCode':200,
    'headers': {'Access-Control-Allow-Origin': '*'},
    'body':  json.dumps(rows)
    }
The folders in my project are (im not even sure how to update)

.idea
_pycache_
pip
pip-9.0.1.dist-info
pkg_resources
pymysql
PyMySQL-0.7.11.dist-info
setuptools
setuptools-36.6.0.dist-info
wheel
wheel-0.30.0.dist.info

With Lambda there is the ability to run Python 3.7

Thanks for any assistance, greatly appreciated.
Reply


Messages In This Thread
Looking for an up to date example to query mysql - by UtiliseIT - Feb-18-2019, 10:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 300 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 700 Jan-20-2024, 04:45 AM
Last Post: 1418
  Mysql and mysql.connector error lostintime 2 739 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,339 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  Date format and past date check function Turtle 5 4,377 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Problem Using SQL Placeholder In MySQL Query AdeS 11 6,284 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,275 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  How to add date and years(integer) to get a date NG0824 4 2,924 Sep-03-2020, 02:25 PM
Last Post: NG0824
  MYSQL Update Query format simdo01 1 2,257 Aug-31-2020, 12:59 AM
Last Post: nilamo
  Python mysql query help please tduckman 4 4,378 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